Java-NumberValue转换为不带货币符号的字符串 [英] Java - NumberValue to String without Currency sign

查看:56
本文介绍了Java-NumberValue转换为不带货币符号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NumberValue设置为当前语言环境格式的字符串.

I am trying to format a NumberValue as a String in the current locale format.

例如,对于Locale.Germany,这些就是我想要得到的结果:

For example, for Locale.Germany these would be the results I am trying to get:

1   -> 1,00
1.1 -> 1,10
0.1 -> 0,10

我尝试过的事情:

String test1 = NumberFormat.getInstance().format(1.1);
// -> 1,1
String test2 = NumberFormat.getCurrencyInstance().format(1.1);
// -> 1,10 €

在没有货币符号的情况下,如何获得与第二个示例相同的格式?或者,如何确保第一个示例的结果始终具有当前语言环境所需的小数位数?

How could I get the same format as the second example, without the currency symbol? Or alternativly, how could I make sure the result from the first example always has the amount of decimals needed for the current locale?

由于有些货币的位数为0、2或3,所以我认为将小数设置为固定金额不会产生正确的结果.

Since there are some currencies that have 0,2 or 3 decimals, I don't think setting the decimals to a fixed amount will have the correct results.

此外,有些货币的符号在前面,而某些货币的后面,因此删掉最后一个字符也可能不起作用.

Also, some currencies have their symbol in the front, and some in the back, so cutting off the last characters will probably not work either.

http://apps.cybersource.com/library/documentation/sbc/quickref/currencies.pdf

推荐答案

java.util.Currency provides the necessary information. A reasonable approach to print currency values without the currency sign in pure Java code is to get a general-purpose NumberFormat instance and configure it to use the number of digits defined by the appropriate Currency object:

NumberFormat numberFormat = NumberFormat.getInstance(myLocale);
Currency currency = Currency.getInstance(myLocale);
numberFormat.setMinimumFractionDigits(currency.getDefaultFractionDigits());

如果您需要使用特定的货币而不是您使用的语言环境的默认货币,请使用

If you need a specific currency as opposed to the default currency of the locale you use, use the String override of Currency.getInstance() instead and provide the appropriate currency code.

这篇关于Java-NumberValue转换为不带货币符号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆