Precision and scale for a Double in java

Carvia Tech | August 03, 2020 | 2 min read | 416 views


Firstly let us understand the difference between Precision and Scale.

If the number is 9232.129394, then:

Precision

represents the total number of digits in unscaled value i.e. for the number 9232.129394, the precision is 4 + 6 = 10

In special case when number is 0, precision is 1.

Scale

Scale can be negative, zero or a positive value.

When scale is positive, it represents the number of digits to the right of the decimal point i.e. 6 in above case (.129394)

When scale is negative, the unscaled value of the number is multiplied by ten to the power of negation of scale. For example, a scale of -2 means the unscaled value is multiplied by 100.

Some examples of precision and scale are:

Number Precision Scale

99.99

4

2

9999999999

10

0

99999.999

8

3

99999000

5

-3

0.00001

1

5

You would never want to lose the precision of the number as it will change the value by a large amount. If you still want to lose the precision simply divide the number by 10 to the power precision.

Set scale in Java

There are multiple ways in Java to round the double value to certain scale, as mentioned in the below example,

BigDecimal rounding in Java
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));
    }
}

Outcome will be same in all the approaches, but the first method of rounding using BigDecimal should be preferred in most scenarios.

RoundingMode.HALF_EVEN

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:
  1. What is Double Checked Locking Problem in Multi-Threading?
  2. Difference between Callable and Runnable Interface
  3. Factorial of a large number in Java BigInteger
  4. Comparable vs Comparator in Java
  5. ThreadLocal with examples in Java
  6. Troubleshooting Deadlock in Java
  7. CRC32 checksum calculation Java NIO

Find more on this topic:

Recommended books for interview preparation:
Buy interview books

Java & Microservices interview refresher for experienced developers.


This website uses cookies to ensure you get the best experience on our website. more info