I've writing an app that requires French language support. I'm having trouble getting currency to format correctly when the locale is set to FR. Is there any way to do this correctly?
Asked
Active
Viewed 137 times
1 Answers
1
I had to do it myself. Created a "Formatter" class extending AFormatter with the following methods Don't now how it is in France but in Argentina we use the following format 10.000.000,18 for example. Hope it's helps or that someone provides a better solution
public static String addCommas(String s) {
int extraRound = 0;
boolean negative = false;
int c;
if(s.charAt(0) == '-') {
negative = true;
s = s.substring(1);
}
int len = s.indexOf('.')==-1?s.length():s.indexOf('.');
if(len > extraRound + 3) {
c = len - extraRound - 3;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 6) {
c = len - extraRound - 6;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 9) {
c = len - extraRound - 9;
s = s.substring(0, c) + "," + s.substring(c);
}
if(len > extraRound + 12) {
c = len - extraRound - 12;
s = s.substring(0, c) + "," + s.substring(c);
}
if(negative) {
s = '-' + s;
}
return s;
}
public static String addCommasNew(String s) {
int extraRound = 0;
boolean negative = false;
int c;
if(s.charAt(0) == '-') {
negative = true;
s = s.substring(1);
}
int len = s.indexOf(',')==-1?s.length():s.indexOf(',');
if(len > extraRound + 3) {
c = len - extraRound - 3;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 6) {
c = len - extraRound - 6;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 9) {
c = len - extraRound - 9;
s = s.substring(0, c) + "." + s.substring(c);
}
if(len > extraRound + 12) {
c = len - extraRound - 12;
s = s.substring(0, c) + "." + s.substring(c);
}
if(negative) {
s = '-' + s;
}
return s;
}
public static String removeScientific(String s) {
int eIndex = s.indexOf('E');
int initialPos = s.indexOf('.');
String result = s;
if (eIndex != -1){
int base = Integer.parseInt(s.substring(eIndex+1));
String pre = s.substring(0, initialPos);
String pos = s.substring(initialPos+1, eIndex);
String pos1 = "";
if (base < pos.length()){
String pos1a = pos.substring(0, base);
String pos1b = pos.substring(base, pos.length());
pos1 = pos1a + "." + pos1b;
} else {
pos1 = pos.substring(0, pos.length());
for (int i = 0; i < base-pos.length(); i++){
pos1 = pos1 + "0";
}
}
result = pre + pos1;
}
return result;
}
public static String changePointToComma(String s){
int initialPos = s.indexOf('.');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + "," + s.substring(initialPos + 1, s.length());
}
return result;
}
public static String changeCommaToPoint(String s){
int initialPos = s.indexOf(',');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + "." + s.substring(initialPos + 1, s.length());
}
return result;
}
public static String removeNumberFormat(String s){
int initialPos = s.indexOf('.');
String result = s;
if (initialPos != -1){
result = s.substring(0, initialPos) + s.substring(initialPos + 1, s.length());
result = Formatter.removeNumberFormat(result);
} else {
return result = Formatter.changeCommaToPoint(s);
}
return result;
}
public static String roundDouble(String s, int presicion){
int initialPos = s.indexOf('.');
String result = s;
String pre;
String pos;
if (initialPos != -1){
pre = s.substring(0, initialPos);
pos = s.substring(initialPos + 1, s.length());
if (presicion < pos.length()){
pos = s.substring(initialPos + 1, initialPos + 1 + presicion );
int dec = Integer.parseInt(pos);
int next = Integer.parseInt(s.substring(initialPos + 1 + presicion, initialPos + 2 + presicion )); //to round the las digit
if (next > 4){
dec = dec + 1;
pos = dec + "";
if ((dec+"").length() > presicion){
pre = (Integer.parseInt(pre) + 1) + "";
pos = "0";
}
}
} else {
}
result = pre + "." + pos;
}
return result;
}
![](../../users/profiles/865466.webp)
Juanma Baiutti
- 647
- 1
- 10
- 27