简体   繁体   中英

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). 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. It's pretty well documented how to format a number.

What Should You use?

You should use 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

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

which is much more handy, (C-Style formatting symbols)

A float has a limited accuracy of approximately 7 to 8 decimal digits. 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). Since your number has more than 7 digits, rounding will chop off the remaining digits.

At the very least, you'll want to use a double to hold the number. With 16 significant decimal digits, it can at least express differences of 0.01 for numbers of that magnitude. However, computations will still be only approximate.

If you need exact representation and computation, use a BigDecimal instead.

Finally, for formatting a number, use a NumberFormat. The Java Tutorial explains that quite well.

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). 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)


float is not the best format for amounts (or most things) I would suggest using double instead (or BigDecimal or long with fixed precision)

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. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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