1

I am devloping an app in which i am getting value like mentioned in statement and i have a value in double as.I need to get three digit value after "." How can i do that

value

9.690000000000001
Dat Nguyen
  • 1,711
  • 15
  • 36
Vishal
  • 67
  • 2
  • 8

5 Answers5

1

you can use String.format("%.3f", d)

This will round your double to 3 decimal place.

Ambrish Pathak
  • 3,408
  • 1
  • 13
  • 28
0

You can use DecimalFormat

Double doub=Double.parseDouble(new DecimalFormat("##.###").format(9.690000000000001));

note It will return only upto two decimal positions if the third digit is zero.

Arpan Sharma
  • 1,982
  • 9
  • 21
0

Use String.format like:

Double formattedDouble = Double.parseDouble(String.format("%.2f", doubleVariable));
Log.d(getClass().getSimpleName(), "Formatted double is: " + formattedDouble);

%.2f rounds the variable up to two decimal places.

Or, you can use java.util.Formatter which works in the same way.

Also,

DecimalFormat df = new DecimalFormat("#.00"); 

Note the 00, meaning exactly two decimal places.

rupinderjeet
  • 2,597
  • 24
  • 44
0

Please try this:

double d = 9.690000000000001;
d = (double)Math.round(d * 1000) / 1000.0;
Dat Nguyen
  • 1,711
  • 15
  • 36
0

there are three ways to do this:

  • get_double = (double)(Math.round(result_value*1000)/1000.0)
  • DecimalFormat df = new DecimalFormat("#.###"); get_double = Double.ParseDouble(df.format(result_value));
  • BigDecimal bd = new BigDecimal(result_value); BigDecimal bd2 = bd.setScale(3, BigDecimal.ROUND_HALF_UP); get_double = Double.ParseDouble(bd2.ToString());
peter zhang
  • 156
  • 4