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
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
you can use String.format("%.3f", d)
This will round your double to 3 decimal place.
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.
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.
Please try this:
double d = 9.690000000000001;
d = (double)Math.round(d * 1000) / 1000.0;
there are three ways to do this: