在我的应用程序中,我希望能够:
让这个数字300000看起来像这样300,000 USD? 我不希望它一定是“USD”,
我也希望能够选择我的自己的货币。
最佳答案
要执行您所描述的操作,请尝试从数字字符串末尾开始计算每 3 个数字(字符),并添加 ','
。然后,在最后添加“USD”(空格使其看起来更好)。
示例代码:
int toFormat = 1563992;
String output = "" + toFormat;
int counter = 0;
for (int index = output.length() - 1; index > 0; index--){
counter++;
if (counter % 3 == 0){
counter = 0;
output = output.subString(0,index) + "," + output.subString(index);
}
}
output += " USD"; // or whatever you want
System.out.println(output);
产出:1,563,992 美元
关于java - 将数字格式更改为以货币格式可读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852241/