我正在编写一个需要法语支持的应用。当语言环境设置为FR时,我无法正确格式化货币。有没有办法正确地做到这一点?
答案 0 :(得分:1)
我必须自己做。 使用以下方法创建了一个扩展AFormatter的“Formatter”类 现在不要在法国如何,但在阿根廷,我们使用以下格式10.000.000,18。 希望它有所帮助或有人提供更好的解决方案
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;
}