[英]java: get different parts of NumberFormat.getCurrencyInstance() result
In Java, with NumberFormat.getCurrencyInstance(), I can get a formatted string representing a price. 在Java中,使用NumberFormat.getCurrencyInstance(),可以获取表示价格的格式化字符串。 eg 例如
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.GERMANY);
System.out.println(nf.format(20.10)); // "20,10 €"
Is there an easy way to get the different part of this formatted string? 是否有一种简单的方法来获取此格式化字符串的不同部分? ie something like 即像
integerPart -> 20
decimalPart -> 10
currencySymbol -> €
decimalSeparator -> ,
Thanks! 谢谢!
You're really asking for two separate things: 您实际上是在要求两件事:
integerPart
and decimalPart
belong to the input value. integerPart
和decimalPart
属于输入值。 They can be calculated with some simple math: 可以使用一些简单的数学公式来计算它们:
double input = 20.10; int integerPart = (int)input; // 20 int decimalPart = (int)((input - integerPart) * 100); // 10
currencySymbol
and decimalSeparator
relate to the output value. currencySymbol
和decimalSeparator
与输出值有关。 They can be retrieved using the DecimalFormatSymbols
class: 可以使用DecimalFormatSymbols
类检索它们:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.GERMANY); String currencySymbol = symbols.getCurrencySymbol(); // € char decimalSeparator = symbols.getDecimalSeparator(); // ,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.