Java 使用 DecimalFormat 格式化数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16583604/
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
Formatting numbers using DecimalFormat
提问by Jon
I am trying to format prices using DecimalFormat, but this isn't working for all variations.
我正在尝试使用 DecimalFormat 格式化价格,但这不适用于所有变体。
DecimalFormat df = new DecimalFormat("0.##")
df.format(7.8)
df.format(85.0)
prints
印刷
7.80
and
和
85
but "7.79999" gets formatted as "7.8", not "7.80". I have tried doing things this way
但是“7.79999”被格式化为“7.8”,而不是“7.80”。我试过这样做
DecimalFormat df = new DecimalFormat("0.00")
to force two dp, but then "85.0" gets formatted as "85.00" not "85"!
强制两个 dp,但随后“85.0”被格式化为“85.00”而不是“85”!
Is there a way of capturing all variations, so that prices are printed either as #, ##, or #.##? For example:
有没有办法捕获所有变化,以便将价格打印为 #、## 或 #.##?例如:
5, 55, 5.55, 5.50, 500, 500.40
5, 55, 5.55, 5.50, 500, 500.40
采纳答案by ntalbs
This doesn't seem to be solved by a single formatter
. I suggest you use "0.00"
format and replace ".00"
with an empty string.
这似乎不是由单个formatter
. 我建议您使用"0.00"
格式并替换".00"
为空字符串。
public static String myFormat(double number) {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(number).replaceAll("\.00$", "");
}
回答by Bathsheba
Use the BigDecimal number class instead:
改用 BigDecimal 数字类:
e.g. if n is a BigDecimal, then you can use
例如,如果 n 是 BigDecimal,那么您可以使用
String s = NumberFormat.getCurrencyInstance().format(n);
By the way, it's best practice to use BigDecimal when working with money.
顺便说一下,最好的做法是在处理金钱时使用 BigDecimal。
回答by Evgeniy Dorofeev
I don't think it's possible, at least not with Java SE formatters. You need to make a custom formatter. I would do it like this
我认为这是不可能的,至少对于 Java SE 格式化程序来说是不可能的。您需要制作自定义格式化程序。我会这样做
String res = df.format(number).replace(".00", "");
回答by Raza
There is a slight difference between these two formats. The "#.##" means it will print the number with maximum two decimal places whereas "#.00" means it will always display two decimal places and if the decimal places are less than two, it will replace them with zeros. see the example below with output.
这两种格式略有不同。“#.##”表示将打印最多两位小数,而“#.00”表示将始终显示两位小数,如果小数位小于两位,它将用零替换。请参阅下面的示例和输出。
public static final DecimalFormat df1 = new DecimalFormat( "#.##" );
public static final DecimalFormat df2 = new DecimalFormat( "#.00" );
System.out.println(df1.format(7.80));
System.out.println(df1.format(85));
System.out.println(df1.format(85.786));
System.out.println(df2.format(7.80));
System.out.println(df2.format(85));
System.out.println(df2.format(85.786));
And the output will be
输出将是
7.8
85
85.79
7.80
85.00
85.79
回答by Jeevitha Pandurangaiah
System.out.println(new java.text.DecimalFormat("#.##").format(5.00));
This will print 5
这将打印 5
System.out.println(new java.text.DecimalFormat("#.00").format(500.401));
This will print 500.40
这将打印 500.40