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
// 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));   } } |
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 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();     } } |
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
// 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);     } } |
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
// 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);     } } |
Precision for Float Values : 1.7922952 Of 1.79