简体   繁体   English

将float格式化为两位小数

[英]formatting float to two decimal format

I'm trying to print out a float value in plain text with two decimal values (if possible). 我正在尝试用两个十进制值(如果可能)以纯文本格式打印出一个浮点值。 For example, if I had 229806210.039999 in the database, then I would like to print it out as 229806210.04, but for some reason it's printing out in scientific notation: 2.29806208E8, which is incorrect in that the last two digit is 08 instead of 10. I tried to convert the number to double and currency format before printing it out but the number is still off (last two digit as 08 instead of 10). 例如,如果我在数据库中有229806210.039999,那么我想将其打印为229806210.04,但是由于某种原因,它以科学计数法打印:2.29806208E8,这是不正确的,因为最后两位是08而不是10我试图在打印出来之前将数字转换为双精度和货币格式,但是数字仍然不可用(最后两位为08而不是10)。 Is there a way to address this issue? 有没有办法解决这个问题? Here's what I have now: 这是我现在所拥有的:

float amount = 0;
amount = something.getAmount() //this will run the query
                              //to retrieve the amount stored in database (i.e. 229806210.039999)
Stringbuilder buffer = new StringBuilder();
buffer.append ("the amount = " + amount)
emailObject.setBody(buffer.toString());
emailService.sendEmail(emailObject);

This looks like Java so see DecimalFormat. 看起来像Java,请参见DecimalFormat。 It's pretty well documented how to format a number. 关于如何格式化数字的文档非常清楚。

What Should You use? 您应该使用什么?

You should use DecimalFormat. 您应该使用DecimalFormat。 It takes a parameter with needed format and returns formatter 它采用具有所需格式的参数并返回格式化程序

Documentation is available here . 文档可在此处获得

Just show me the code! 只是给我看代码!

Double number = 1144.034;
DecimalFormat format = new DecimalFormat("0.00");
System.out.println(format.format(price));

And the output is 输出是

1144.03

DecimalFormat gives (sometimes unwanted) locale decimal points ("." vs., ",") recently i changed to use 最近我更改为使用DecimalFormat给出(有时是不需要的)区域设置小数点(“。”与“,”)

double x= ...;
String formatted = String.format(Locale.US, "%.2d", x);

which is much more handy, (C-Style formatting symbols) 更加方便((C样式格式符号)

A float has a limited accuracy of approximately 7 to 8 decimal digits. 浮点数的精度有限,大约为7到8个十进制数字。 Once you store a value in a float you cannot expect more significant digits no matter how you post process it. 将值存储到浮点型后,无论您如何对其进行后期处理,都无法期望获得更高的有效数字。

Floating point numbers are for approximate calculations with a fixed number of significant digits. 浮点数用于使用固定数量的有效数字进行近似计算。 That is, a float will only store the exponent of the number, and the first 24 binary digits of the number (which is about 7 decimal digits). 也就是说, float将只存储数字的指数和数字的前24个二进制数字(大约为7个十进制数字)。 Since your number has more than 7 digits, rounding will chop off the remaining digits. 由于您的电话号码有7位以上的数字,因此四舍五入会去除剩余的数字。

At the very least, you'll want to use a double to hold the number. 至少,您需要使用double精度数字来保存数字。 With 16 significant decimal digits, it can at least express differences of 0.01 for numbers of that magnitude. 具有16位有效的十进制数字,对于该大小的数字,它至少可以表示0.01的差异。 However, computations will still be only approximate. 但是,计算仍将仅是近似的。

If you need exact representation and computation, use a BigDecimal instead. 如果需要精确的表示和计算,请改用BigDecimal

Finally, for formatting a number, use a NumberFormat. 最后,要格式化数字,请使用NumberFormat。 The Java Tutorial explains that quite well. Java教程对此进行了很好的解释。

I tried to convert the number to double and currency format before printing it out but the number is still off (last two digit as 08 instead of 10). 我试图在打印出来之前将数字转换为双精度和货币格式,但是数字仍然不可用(最后两位为08而不是10)。 Is there a way to address this issue? 有没有办法解决这个问题?

Casting it to a double after the fact is too late, the information has been lost. 事实为时已晚,将其转换为两倍,则信息已丢失。

You have to use double, ie 64-bit floating point every where the value might have passed (Or for a database you can use fixed precision) 您必须在可能传递值的每个地方使用双精度(即64位浮点)(或者对于数据库,您可以使用固定精度)


float is not the best format for amounts (or most things) I would suggest using double instead (or BigDecimal or long with fixed precision) float不是金额(或大多数事物)的最佳格式,我建议改用double(或者BigDecimal或long且具有固定精度)

Instead of 代替

Stringbuilder buffer = new StringBuilder();
buffer.append ("the amount = " + amount)
emailObject.setBody(buffer.toString());

You can write 你可以写

emailObject.setBody(String.format("the amount = %.2f%n" , amount));

It might be "polite" to have at least one newline. 至少有一个换行符可能是“有礼貌”的。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM