Precision 4, scale 2: 99.99 Precision 10, scale 0: 9999999999 Precision 8, scale 3: 99999.999 Precision 5, scale 3: 99999000
Set precision and scale for a double value in java
Carvia Tech  December 04, 2019  2 min read  6 views
Firstly let us understand the difference between Precision and Scale.
If the number is 9232.129394, then
 Precision

represents the number of number of significant digits to which a number is calculated i.e. 4 digits (9232)
 Scale

represents the number of digits to the right of the decimal point i.e. 6 in above case (.129394)
Some other examples are:
No one wants to loose the precision of the number as it will change the value by large amount. If you still want to loose the precision simply divide the number by 10 to the power precision.
There are multiple ways in Java to round the double value to certain scale, as mentioned in the below example
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class RoundDouble {
public double round1(double input, int scale) {
BigDecimal bigDecimal = new BigDecimal(input).setScale(scale, RoundingMode.HALF_EVEN);
return bigDecimal.doubleValue();
}
public double round2(double input) {
return Math.round(input * 100) / 100.0d;
}
public double round3(double input) {
DecimalFormat df = new DecimalFormat("#.00");
return Double.parseDouble(df.format(input));
}
public static void main(String[] args) {
RoundDouble rd = new RoundDouble();
System.out.println(rd.round1(9232.129394d, 2));
System.out.println(rd.round2(9232.129394d));
System.out.println(rd.round3(9232.129394d));
}
}
The first method of rounding using BigDecimal should be preferred in most scenarios.
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it’s even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as "Banker’s rounding," and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.
Top articles in this category:
 What is Double Checked Locking Problem in MultiThreading?
 What is ThreadLocal in Java, where will you use this class
 What do you understand by Java Memory Model?
 can we write a java method that swaps two integers
 What is volatile keyword in Java
 Is Java Pure Object Oriented Language?
 What is difference between HashMap and HashSet
Find more on this topic:
Core Java
Core Java  OOP Concepts, Garbage Collection, Multithreading, Collections Framework, Java 8 Features, Lambda Functions, Streams.
Last updated 1 week ago
Recommended books for interview preparation:
Similar Posts
 Explain Java Exception Class Hierarchy
 Http download using Java NIO FileChannel
 CRC32 checksum calculation Java NIO
 Set precision and scale for a double value in java
 Difference between HashMap, LinkedHashMap and TreeMap
 What is difference between ExecutorService submit and execute method
 What is left shift right shift and unsigned rght shift operator in Java
 What happens when wait() & notify() method are called
 can we write a java method that swaps two integers
 Find missing numbers in 4 billion unique numbers with 50MB RAM
Enter your email address to subscribe to this blog and receive notifications of new posts by email.