How to Set Precision For Double Values in Java?

04 Jul, 2022
Significant digits are referred to all digits inclusive of left and right to decimal place keeping a note adding 0 to left of any number is not countable as significant digits whereas precise digits referred to digits that are only lying on the right side or in short after the decimal place in mathematics. In Java, there are numbers more than 16 numbers only to the precision but can go more. Here we are given a double value, the task is to set its precision value to specific decimal places. It is illustrated below illustrations as follows:
Illustrations: 
Input  : val = 1 
Output : 1.0000
Upto 4 decimal places
Input  : 12.5
Output : 12.500000
Upto 6 decimal places
Using format() Method of String classUsing round() method of Math class
Using format() Method of String class
Using round() method of Math class
We can use format() method of String class to format the decimal number to some specific format.
Syntax:
String.format("%.Df", decimalValue);
// Where D is the number required number of Decimal places
Example-1:
Java
// Java Program to Illustrate format() Method// of String class // Importing required classesimport java.io.*;import java.lang.*; // Classclass GFG {     // Main driver method  public static void main(String[] args) {      // Declaring and initializing     // double value    double a = 0.9;     // Setting the precision    // to 20 places    System.out.println(      String.format("%.20f", a));     double b = 1;     // Setting the precision    // to 5 places    System.out.println(      String.format("%.5f", b));  }}
0.90000000000000000000
1.00000
From the above output, it is clear that precision of 20 digits is been carried out for the first entry whereas precision to 5 digits is carried out on input double value. 
Example-2:
Java
// Demonstrating the precision modifier import java.util.*; class GFG {    public static void main (String[] args) {      Formatter fm=new Formatter();             // Format 4 decimal places      fm.format("%.4f", 123.1234567);      System.out.println(fm);      fm.close();             //Format 2 decimal places in a 16 charcater field      fm=new Formatter();      fm.format("%16.2e",123.1234567);      System.out.println("GFG!");      fm.close();             //Display atmost 15 characters in a string      fm=new Formatter();      fm.format("%.15s", "Learning with Gfg is easy quick");      System.out.println(fm);      fm.close();    }}
123.1235
GFG!
Learning with G
Example:
Java
// Java Program to Illustrate Precision Setting In Double// Using round() Method of Math Class // Importing required classesimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Declaring and initializing double variable        double num = 3.141414141414;         // Rounding off above double number        // to 7 precision        double ans            = Math.round(num * 10000000) / 10000000.0;         // Printing the above precised value        // of double value        System.out.println(ans);    }}
3.1414141
The above double number is precise to 7 digits which can easily be seen from the output generated. 
snape_here
solankimayank
singhankitasingh066
Java-Data Types
Java-Double
Java-Strings
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java