-1

I want to round a number to 4 decimals. I have done that :

roundedNumber =  (double)(Math.round(roundedNumber)*10000)/10000;

It doesn't work with a number such as : 0.20425 . I am getting the following output: 0.0

What should I do to get: 0.2042 ?

bish
  • 3,075
  • 8
  • 41
  • 62
user1260928
  • 2,959
  • 6
  • 52
  • 94

2 Answers2

2

Use DecimalFormat

DecimalFormat decimalFormat = new DecimalFormat("#.####");
double formatedNumber =  Double.valueOf(decimalFormat.format(0.20425));

System.out.println(formatedNumber);
codeaholicguy
  • 1,611
  • 1
  • 10
  • 17
1

You should mutiply before rounding:

roundedNumber = Math.round(roundedNumber*10000)/10000.0;
Tagir Valeev
  • 87,515
  • 18
  • 194
  • 305