How to set Precision for Double values in Java?
Given a double value val, the task is to set its precision value to a specific decimal places.
Input: val = 1 Output: 1.0000 Upto 4 decimal places Input : 12.5 Output : 12.500000 Upto 6 decimal places
Approach: Using String.format()
We can use String.format() method to format the decimal number to some specific format.
String.format("%.Df", decimalValue); where D is the number required number of Decimal places.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.