java - 将数字格式更改为以货币格式可读?

标签 java android

在我的应用程序中,我希望能够:

让这个数字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/

相关文章:

Java调整图像的中心

java - NiftyGui - 如何改变面板的大小

android - gradle错误:无法连接到服务器

java - 从括号部分拉出变量

Java - 为什么静态 block 中的方法没有及时加载?

android - 如何: Define theme (style) item for custom widget

android - 深度链接不会在 Android 中自动打开

java - 在 libGDX 中的矩形中绘制动画纹理

java - jcl-over-slf4j : set logging level howto

android uiautomator点击ListView