-1

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);
user974047
  • 2,115
  • 7
  • 33
  • 45
  • 1
    The first question is if float has the precission needed to represent such number without altering it. My guess is that it does not. Check how float values are encoded. Probably you will end using `BigDecimal` – SJuan76 Jan 07 '13 at 21:25
  • 1
    Duplicate of http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Aaron Kurtzhals Jan 07 '13 at 21:27
  • 1
    `229806210.04` is too large to be a float. You would lose precision. Try using `double` instead. – Peter Lawrey Jan 07 '13 at 21:40

6 Answers6

3

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
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
2

This looks like Java so see DecimalFormat. It's pretty well documented how to format a number.

john_omalley
  • 1,398
  • 6
  • 13
2

DecimalFormat gives (sometimes unwanted) locale decimal points ("." vs., ",") recently i changed to use

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

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

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

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.

Henry
  • 42,982
  • 7
  • 68
  • 84
0

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.

meriton
  • 68,356
  • 14
  • 108
  • 175
0

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, i.e. 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. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130