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?

asked Feb 12, 2014 at 11:48
1

5 Answers 5

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
answered Feb 12, 2014 at 11:51
Sign up to request clarification or add additional context in comments.

6 Comments

So why to use huge BigDecimal for little numbers? Why to not use normal formatting?
-1 for totally unnecessary BigDecimal. The second part of your answer looks good though.
@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.
@Ɍ.Ɉ 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 - I've edited my answer a bit though I still stand for what I said regarding BigDecimal. FYI, no offense taken :)
|
1
DecimalFormat f=new DecimalFormat("0.00");
String formate = f.format(value); 
double finalValue = (Double)f.parse(formate) ;
System.out.println(finalValue);
answered Feb 12, 2014 at 11:52

Comments

1

Use setRoundingMode as:

f.setRoundingMode( RoundingMode.DOWN );

How to round a number to n decimal places in Java

answered Feb 12, 2014 at 12:14

Comments

0

try this may be helpful:

 double d = 42.405;
 System.out.println(String.format("%2.2f", d));
answered Feb 12, 2014 at 11:55

Comments

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.

answered Feb 12, 2014 at 12:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.