Java 十进制格式的模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3538311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pattern for Decimal Format
提问by Cheok Yan Cheng
I would like to translate 10000000.0
to PREFIX10,000,000.00
我想翻译10000000.0
成PREFIX10,000,000.00
May I know what pattern I should pass into DecimalFormat?
我可以知道我应该将什么模式传递给 DecimalFormat 吗?
NumberFormat numberFormat = new DecimalFormat(...);
numberFormat.format(10000000.0);
采纳答案by Matthew Flaschen
new DecimalFormat("PREFIX#,##0.00");
This will always show at least one integer digit (e.g. 0), and use comma as group separator.
这将始终显示至少一个整数(例如 0),并使用逗号作为组分隔符。
回答by akf
The pattern for the number would be: "#,###.00"
which means:
数字的模式是:"#,###.00"
这意味着:
- use a grouping separator, based on locale
- use a decimal separator, based on locale
- always have 2 places after the decimal separator
- have as many whole number digits as is necessary
- 根据区域设置使用分组分隔符
- 根据区域设置使用小数点分隔符
- 小数点分隔符后总是有 2 个位置
- 有必要的整数位数
The prefix can be an arbirary string. If it is a currency symbol, there is a special notation for that. For more info on the definition of patterns, see the DecimalFormat JavaDoc
前缀可以是任意字符串。如果它是货币符号,则有一个特殊的符号。有关模式定义的更多信息,请参阅DecimalFormat JavaDoc