46

Possible Duplicate:
How to round a number to n decimal places in Java

I am having difficulties rounding a float to two decimal places. I have tried a few methods I have seen on here including simply just using Math.round(), but no matter what I do I keep getting unusual numbers.

I have a list of floats that I am processing, the first in the list is displayed as 1.2975118E7. What is the E7?

When I use Math.round(f) (f is the float), I get the exact same number.

I know I am doing something wrong, I just am not sure what.

I just want the numbers to be in the format x.xx. The first number should be 1.30, etc.

Community
  • 1
  • 1
lonewookie
  • 697
  • 1
  • 7
  • 10
  • 2
    You want to round the String *display* of a floating point number. You likely don't want to round the number itself. Avoid float, and use double instead for greater precision, and then look at one of the many methods available to give decent String display of a double number including String.format(...), NumberFormat or DecimalFormat. – Hovercraft Full Of Eels Jun 17 '12 at 15:19

4 Answers4

125

1.2975118E7 is scientific notation.

1.2975118E7 = 1.2975118 * 10^7 = 12975118

Also, Math.round(f) returns an integer. You can't use it to get your desired format x.xx.

You could use String.format.

String s = String.format("%.2f", 1.2975118);
// 1.30
Alexander
  • 23,432
  • 11
  • 63
  • 73
  • 2
    `String.format` can generate problems since it will create float numbers with dots or commas according to the locale. – Yoann Hercouet Feb 29 '16 at 14:08
  • @YoannHercouet, you can't go around playing with floats without thinking about locales. That shouldn't be a problem. ;-) – Alexander Mar 02 '16 at 12:49
  • 5
    use this : String.format(Locale.US, "%.2f", 1.2975118) which is safer .. at least you know the format always... – Maher Abuthraa Oct 25 '16 at 11:16
55

If you're looking for currency formatting (which you didn't specify, but it seems that is what you're looking for) try the NumberFormat class. It's very simple:

double d = 2.3d;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String output = formatter.format(d);

Which will output (depending on locale):

$2.30

Also, if currency isn't required (just the exact two decimal places) you can use this instead:

NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String output = formatter.format(d);

Which will output 2.30

cyrilchampier
  • 2,207
  • 1
  • 25
  • 39
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Not sure. I up-voted you and @Makato as these were the first to address the real problem: the formatted String representation of a floating point number. – Hovercraft Full Of Eels Jun 17 '12 at 15:39
  • Nice answer, BUT I have a problem with it :)). An issue that I didn't quite yet find an answer. It's a string, and the server is waiting for me to send a float through. I tried using Float.valueOf() or parseFloat(): if its 0.10003482948329 it will get me 0.10 which I need. But if it's 2.00000000000 it will get me 2, and I need 2.00 (must be a floating point number) – rosu alin Mar 24 '16 at 09:04
  • @rosualin , it might be not a good way, but if you wan just to display this in UI instead of using it for calculation, then , `String.valueOf(RESULT[2])+".00"` Then again `parseFloat` **:-P** – exploitr May 21 '18 at 09:31
9

You can make use of DecimalFormat to give you the style you wish.

DecimalFormat df = new DecimalFormat("0.00E0");
double number = 1.2975118E7;
System.out.println(df.format(number));  // prints 1.30E7

Since it's in scientific notation, you won't be able to get the number any smaller than 107 without losing that many orders of magnitude of accuracy.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks to everyone for your help. Scientific notation was what got me, I thought it wasn't working because the numbers were the same, but my number were wrong to begin with! – lonewookie Jun 17 '12 at 16:48
  • can i return float value from this : float roundofDecimal(float dd){ DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); System.out.println(df.format(dd)); return df.format(dd); } – CoronaPintu Jul 01 '13 at 06:27
  • 1
    @PintuCorna: No - doesn't look like there's any way to get the float back from a `DecimalFormat`. In all honesty, you're losing precision if you're going from a double to a float; also, formatting the number is a formality for the person reading the data - if you want the raw floating point number, just return *that*! – Makoto Jul 01 '13 at 06:32
  • ok then any way becuse i want exact two decimal point in float – CoronaPintu Jul 01 '13 at 07:02
0

Try looking at the BigDecimal Class. It is the go to class for currency and support accurate rounding.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77