Posted By: Anonymous
I was working with numbers recently and I had a situation where I want to set the precision of a double value say to 6 digits or 4 digits, depending on the value stored in the database.
For example, If in the database the precision is set as 4 digits then the output must look as,
I tried with
DecimalFormat and using the string
##.####, but it is annoying to use symbols everytime.
Is there any better approach, say something like below:
Double value = 10.0; value.setPrecision(4);
You can’t set the precision of a double (or Double) to a specified number of decimal digits, because floating-point values don’t have decimal digits. They have binary digits.
You will have to convert into a decimal radix, either via
DecimalFormat, depending on what you want to do with the value later.
See also my answer to this question for a refutation of the inevitable *100/100 answers.