How to Set Precision For Double Values in Java?
Significant digits refer to all digits inclusive of left and right to decimal place keeping a note adding 0 to the left of any number is not countable as significant digits whereas precise digits refer 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 placesInput : 12.5
Output : 12.500000 Upto 6 decimal places
Different Methods to Set Precision in Double Values
Method 1: Using the format() Method of the String class
We can use the format() method of the 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 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:
// 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 character 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
2. Using round() method of Math Class
Example 1: ( Precision for Double )
Below is the implementation of the above method:
// 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.
Example 2: ( Precision for Float )
Below is the implementation of the above topic:
// Java Program to demonstrate
// Precision of float
import java.io.*;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// Assigned value to f1
Float f1 = 4.024151f;
// Assigned value to f2
Float f2 = 2.24525f;
Float result = f1 / f2;
// Simple division of
// two floats f1 and f2.
System.out.println("Precision for Float Values : ");
System.out.print(result + " Of ");
// formate and print method will print
// on single line using the space
// betwee both lines.
System.out.format(" %.2f ", result);
}
}
Output
Precision for Float Values : 1.7922952 Of 1.79