1

I'm using

double i2 = value * 2.23694;
i2 = (double)(Math.round(i2 * 100)) / 100;

for rounding doubles. But it rounds to only 2 decimal places.

I want it to be 6 decimal places.

Is there any way to use Math.round and have 6 decimal places?

bobbel
  • 3,327
  • 2
  • 26
  • 43
user3456904
  • 73
  • 1
  • 1
  • 6
  • What do you expect? If you round a number, you're removing all decimal places. After that, when you're dividing it with 100, the only possible result is two decimal places! – bobbel Apr 03 '14 at 09:47

3 Answers3

13

You are casting things to Integers which will ruin any rounding. To use doubles, use a decimal point (i.e 100.0 instead of 100). And if you want it with 6 decimals, use 1000000.0 like this:

 double i2 = value * 2.23694; 
 i2 = Math.round(i2*1000000.0)/1000000.0;

But generally I think DecimalFormat is a more elegant solution (guessing you want it rounded only to present it):

DecimalFormat f = new DecimalFormat("##.000000");
String formattedValue = f.format(i2);
ddmps
  • 4,350
  • 1
  • 19
  • 34
6

If you are using the values for displaying just use below method for rounding to 6 digits

double a = 12.345694895;
String str = String.format("%.6f", a );
Riskhan
  • 4,434
  • 12
  • 50
  • 76
1

double value = 12.3464367843; double rounded = (double) Math.round(value * 1000000) / 1000000;

output:12.346437

Sarojini2064130
  • 221
  • 3
  • 7