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 places
Input : 12.5
Output : 12.500000
Upto 6 decimal places
Different Methods to Set Precision in Double Values
- Using format() Method of String class
- Using round() method of Math class
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
import java.io.*;
import java.lang.*;
class GFG {
public static void main(String[] args) {
double a = 0.9 ;
System.out.println(
String.format( "%.20f" , a));
double b = 1 ;
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
import java.util.*;
class GFG {
public static void main (String[] args) {
Formatter fm= new Formatter();
fm.format( "%.4f" , 123.1234567 );
System.out.println(fm);
fm.close();
fm= new Formatter();
fm.format( "%16.2e" , 123.1234567 );
System.out.println( "GFG!" );
fm.close();
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
double num = 3.141414141414 ;
double ans
= Math.round(num * 10000000 ) / 10000000.0 ;
System.out.println(ans);
}
}
|
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
Float f1 = 4 .024151f;
Float f2 = 2 .24525f;
Float result = f1 / f2;
System.out.println( "Precision for Float Values : " );
System.out.print(result + " Of " );
System.out.format( " %.2f " , result);
}
}
|
Output
Precision for Float Values :
1.7922952 Of 1.79
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Sep, 2023
Like Article
Save Article