# How to Set Precision For Double Values in Java?

• Difficulty Level : Easy
• Last Updated : 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```

### Method 1: Using the format() Method of the String 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 classes``import` `java.io.*;``import` `java.lang.*;` `// Class``class` `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));``  ``}``}`

Output

```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();``    ``}``}`

Output

```123.1235
GFG!
Learning with G```

Example:

## Java

 `// Java Program to Illustrate Precision Setting In Double``// Using round() Method of Math Class` `// Importing required classes``import` `java.util.*;` `// Class``class` `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);``    ``}``}`

Output

`3.1414141`

The above double number is precise to 7 digits which can easily be seen from the output generated.

My Personal Notes arrow_drop_up