简体   繁体   English

Java:获取NumberFormat.getCurrencyInstance()结果的不同部分

[英]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: 您实际上是在要求两件事:

  1. integerPart and decimalPart belong to the input value. integerPartdecimalPart属于输入值。 They can be calculated with some simple math: 可以使用一些简单的数学公式来计算它们:

     double input = 20.10; int integerPart = (int)input; // 20 int decimalPart = (int)((input - integerPart) * 100); // 10 
  2. currencySymbol and decimalSeparator relate to the output value. currencySymboldecimalSeparator输出值有关。 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.

相关问题 修改NumberFormat.getCurrencyInstance()返回的NumberFormat - Modifying the NumberFormat returned by NumberFormat.getCurrencyInstance() NumberFormat.getCurrencyInstance()将所有内容都归零? - NumberFormat.getCurrencyInstance() turning everything to zero? NumberFormat.getCurrencyInstance() 格式化为带空格的字符串,即没有空格。 如何删除它? - NumberFormat.getCurrencyInstance() formats to a string with whitespace, that is no whitespace. How to remove it? 如何将 NumberFormat.getCurrencyInstance(Locale.US) 转换为 Double - How to convert NumberFormat.getCurrencyInstance(Locale.US) to Double NumberFormat.getCurrencyInstance()的Currency格式基于什么标准? - What standard is the Currency formatting of NumberFormat.getCurrencyInstance() based on? NumberFormat.getCurrencyInstance() 不返回语言环境中国和法国的货币符号 (jdk-1.8) - NumberFormat.getCurrencyInstance() not returning currency symbol for Locales CHINA and FRANCE (jdk-1.8) Servlet全球化NumberFormat.getCurrencyInstance(Locale.JAPAN)无法正常工作 - Servlet Globalization NumberFormat.getCurrencyInstance(Locale.JAPAN) doen't work properly 如何在Java / Kotlin中获取NumberFormat的语言环境? - How to get the Locale of a NumberFormat in Java/Kotlin? Java NumberFormat - Java NumberFormat 使用Java中的getCurrencyInstance()获取要显示的特定符号 - Getting Specific symbols to appear using getCurrencyInstance() in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM