0

Currently I'm using DecimalFormat class to round off double value

double d = 42.405;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));

output: 42.41;

I'm doing browser app testing using Selenium, so based on the browser I need to round off the value.

For Example:

IE rounds off 42.405 to 42.40 and others rounds off to 42.41. But if values are like 42.403, 42.406 then I see consistency across all browsers. So now I have to put a condition in my script so if browser is IE then round off should happen in such a way that I should get 42.40 and for other browsers is should get 42.41. How can i do this?

Muthu
  • 449
  • 2
  • 7
  • 13
  • possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Peter O. Sep 08 '14 at 07:01

5 Answers5

3

You can specify the RoundingMode for the DecimalFormatter, but please choose it as per your needs(I've just given an example using HALF_UP).

double d = 42.405;
DecimalFormat f = new DecimalFormat("##.00");
f.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(f.format(d)); // Prints 42.41

Alternatively, you can also use BigDecimal(incase you know why we usually go for BigDecimal instead of double) for the same.

double d = 42.405;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println(bd.doubleValue()); // Prints 42.41
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • So why to use huge `BigDecimal` for little numbers? Why to not use normal formatting? – Ashot Karakhanyan Feb 12 '14 at 11:56
  • -1 for totally unnecessary `BigDecimal`. The second part of your answer looks good though. – SebastianH Feb 12 '14 at 12:11
  • @SebastianH - How do you call that unnecessary? It may not be useful to you, or probably even the OP, but I'm sure it'd be useful to future users who come by. I gave both the options and its all on the OP to use the one they require. I respect your vote but I'd not take down the `BigDecimal` part from the answer. – Rahul Feb 12 '14 at 12:14
  • @Ɍ.Ɉ I believe you should only use BigDecimal if there is a need for it (e.g. double not beeing precise enough). The average "future user" of this question is not looking for high precision arithmetics but for basic rounding. So please dont take offense, but I find the first part of your answer very misleading. – SebastianH Feb 12 '14 at 12:27
  • @SebastianH - I've edited my answer a bit though I still stand for what I said regarding `BigDecimal`. FYI, no offense taken :) – Rahul Feb 12 '14 at 12:34
  • You should really use the BigDecimal String constrictor. ie `bd = new BigDecimal("42.405")` otherwise you may have already lost precision by the time 42.405 is stored as a double. – lance-java Feb 12 '14 at 12:51
1
DecimalFormat f=new DecimalFormat("0.00");
String formate = f.format(value); 
double finalValue = (Double)f.parse(formate) ;
System.out.println(finalValue);
Sathesh
  • 378
  • 1
  • 4
  • 13
1

Use setRoundingMode as:

f.setRoundingMode( RoundingMode.DOWN );

How to round a number to n decimal places in Java

Community
  • 1
  • 1
Maxim
  • 339
  • 1
  • 5
0

try this may be helpful:

 double d = 42.405;
 System.out.println(String.format("%2.2f", d));
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
0

Also you can do it "handly" as follow

double d = 42.405;
final double roundedValue = (Math.round(d * 100) / (double) 100);

In case of 42.405 you get 42.41 and in case of 42.404 - 42.4 And so after

System.out.println(String.format("%2.2f", roundedValue));

you will get necessary output. 42.41 or 42.40 correspondingly.

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28