Set Specific Precision of a Bigdecimal

Set specific precision of a BigDecimal

The title of the question asks about precision. BigDecimal distinguishes between scale and precision. Scale is the number of decimal places. You can think of precision as the number of significant figures, also known as significant digits.

Some examples in Clojure.

(.scale     0.00123M) ; 5
(.precision 0.00123M) ; 3

(In Clojure, The M designates a BigDecimal literal. You can translate the Clojure to Java if you like, but I find it to be more compact than Java!)

You can easily increase the scale:

(.setScale 0.00123M 7) ; 0.0012300M

But you can't decrease the scale in the exact same way:

(.setScale 0.00123M 3) ; ArithmeticException Rounding necessary

You'll need to pass a rounding mode too:

(.setScale 0.00123M 3 BigDecimal/ROUND_HALF_EVEN) ;
; Note: BigDecimal would prefer that you use the MathContext rounding
; constants, but I don't have them at my fingertips right now.

So, it is easy to change the scale. But what about precision? This is not as easy as you might hope!

It is easy to decrease the precision:

(.round 3.14159M (java.math.MathContext. 3)) ; 3.14M

But it is not obvious how to increase the precision:

(.round 3.14159M (java.math.MathContext. 7)) ; 3.14159M (unexpected)

For the skeptical, this is not just a matter of trailing zeros not being displayed:

(.precision (.round 3.14159M (java.math.MathContext. 7))) ; 6 
; (same as above, still unexpected)

FWIW, Clojure is careful with trailing zeros and will show them:

4.0000M ; 4.0000M
(.precision 4.0000M) ; 5

Back on track... You can try using a BigDecimal constructor, but it does not set the precision any higher than the number of digits you specify:

(BigDecimal. "3" (java.math.MathContext. 5)) ; 3M
(BigDecimal. "3.1" (java.math.MathContext. 5)) ; 3.1M

So, there is no quick way to change the precision. I've spent time fighting this while writing up this question and with a project I'm working on. I consider this, at best, A CRAZYTOWN API, and at worst a bug. People. Seriously?

So, best I can tell, if you want to change precision, you'll need to do these steps:

  1. Lookup the current precision.
  2. Lookup the current scale.
  3. Calculate the scale change.
  4. Set the new scale

These steps, as Clojure code:

(def x 0.000691M) ; the input number
(def p' 1) ; desired precision
(def s' (+ (.scale x) p' (- (.precision x)))) ; desired new scale
(.setScale x s' BigDecimal/ROUND_HALF_EVEN)
; 0.0007M

I know, this is a lot of steps just to change the precision!

Why doesn't BigDecimal already provide this? Did I overlook something?

How to set specific precision to BigDecimal in Java?

You can use the rounding mode constant ROUND_DOWN in setScale. However, the overloaded setScale method that takes a RoudingMode is preferred.

BigDecimal price = new BigDecimal("100500100500.9999999")
.setScale(2, RoundingMode.DOWN);

Output:

100500100500.99

BigDecimal, precision and scale

A BigDecimal is defined by two values: an arbitrary precision integer and a 32-bit integer scale. The value of the BigDecimal is defined to be unscaledValue*10^{-scale}.

Precision:

The precision is the number of digits in the unscaled value.
For instance, for the number 123.45, the precision returned is 5.

So, precision indicates the length of the arbitrary precision integer. Here are a few examples of numbers with the same scale, but different precision:

  • 12345 / 100000 = 0.12345 // scale = 5, precision = 5
  • 12340 / 100000 = 0.1234 // scale = 4, precision = 4
  • 1 / 100000 = 0.00001 // scale = 5, precision = 1

In the special case that the number is equal to zero (i.e. 0.000), the precision is always 1.

Scale:

If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.

This means that the integer value of the ‘BigDecimal’ is multiplied by 10^{-scale}.

Here are a few examples of the same precision, with different scales:

  • 12345 with scale 5 = 0.12345
  • 12345 with scale 4 = 1.2345
  • 12345 with scale 0 = 12345
  • 12345 with scale -1 = 123450

BigDecimal.toString:

The toString method for a BigDecimal behaves differently based on the scale and precision. (Thanks to @RudyVelthuis for pointing this out.)

  • If scale == 0, the integer is just printed out, as-is.
  • If scale < 0, E-Notation is always used (e.g. 5 scale -1 produces "5E+1")
  • If scale >= 0 and precision - scale -1 >= -6 a plain decimal number is produced (e.g. 10000000 scale 1 produces "1000000.0")
  • Otherwise, E-notation is used, e.g. 10 scale 8 produces "1.0E-7" since precision - scale -1 equals unscaledValue*10^{-scale} is less than -6.

More examples:

  • 19/100 = 0.19 // integer=19, scale=2, precision=2
  • 1/1000 = 0.0001 // integer=1, scale = 4, precision = 1

Set all BigDecimal operations to a certain precision?

(Almost) Original

Not as simple, but you can create a MathContext and pass it to all your BigDecimal constructors and the methods performing operations.

Revised

Alternatively, you can extend BigDecimal and override any operations you want to use by supplying the right MathContext, and using the rounding version of divide:

public class MyBigDecimal extends BigDecimal {

private static MathContext context = new MathContext(120, RoundingMode.HALF_UP);

public MyBigDecimal(String s) {
super(s, context);
}
public MyBigDecimal(BigDecimal bd) {
this(bd.toString()); // (Calls other constructor)
}
...
public MyBigDecimal divide( BigDecimal divisor ){
return new MyBigDecimal( super.divide( divisor, context ) );
}
public MyBigDecimal add( BigDecimal augend ){
return new MyBigDecimal( super.add( augend ) );
}
...
}

Java BigDecimal: How to set scale only if it's greater than certain precision point?

It seems like the question you really are asking is "How do I truncate the decimal portion of a BigDecimal so that it has at most 4 numbers to the right of the decimal point?"

First, I think you should ensure you understand what scale and precision mean with regards to a BigDecimal.

If you run this small program:

public static void main(String[] args) {
List<BigDecimal> decimals = new LinkedList<>();
decimals.add( new BigDecimal(".123456"));
decimals.add( new BigDecimal("1.23456"));
decimals.add( new BigDecimal("12.3456"));
decimals.add( new BigDecimal("123.456"));
decimals.add( new BigDecimal("1234.56"));
decimals.add( new BigDecimal("12345.6"));
decimals.add( new BigDecimal("123456"));

for(BigDecimal bd : decimals){
System.out.println(bd.toPlainString() + ". scale = " + bd.scale() + ", precision = " + bd.precision());
}
}

You'll see the following output:

0.123456. scale = 6, precision = 6 
1.23456. scale = 5, precision = 6
12.3456. scale = 4, precision = 6
123.456. scale = 3, precision = 6
1234.56. scale = 2, precision = 6
12345.6. scale = 1, precision = 6
123456. scale = 0, precision = 6

You should now see that what you want to do is query and then adjust the scale of the number.

static BigDecimal doIt(BigDecimal input){
if(input.scale() > 4){
return input.setScale(4, RoundingMode.HALF_DOWN);
}
else{
return input;
}
}

A little test program shows the output is what you requested:

public static void main(String[] args) {
List<BigDecimal> decimals = new LinkedList<>();
decimals.add(new BigDecimal("123.123"));
decimals.add(new BigDecimal("123.123456"));
decimals.add(new BigDecimal("123"));
decimals.add(new BigDecimal("0.1234445"));

for (BigDecimal bd : decimals) {
System.out.println(bd.toPlainString() + " ---> " + doIt(bd));
}
}

Output

123.123 ---> 123.123
123.123456 ---> 123.1235
123 ---> 123
0.1234445 ---> 0.1234

Why is there no BigDecimal.setPrecision() method?

The powers-that-be declared that the overall precision (total # of digits) can be adjusted only incidentally by changing the scale (# of digits to the right of the decimal point).

This is appropriate, as any change to precision necessitates a change to scale.
So at best setPrecision is redundant;
having only setScale has resulted in a cleaner API.

Now consider a change to precision and how the result might need to be rounded.
In going through that analysis,
you will consider "how many digits should remain after rounding".
That is the scale.
Thus any such consideration leads back to the question of how the scale
should be changed.

setScale with its variants to specify rounding are a natural fit.
Less so for a hypothetical setPrecision, which begs for code comments to fill the gap (IMHO).

Convert double to BigDecimal and set BigDecimal Precision

It prints 47.48000 if you use another MathContext:

BigDecimal b = new BigDecimal(d, MathContext.DECIMAL64);

Just pick the context you need.

Set n decimal places in BigDecimal and format the number Java

As mentioned by @JB Nizet, you need to tune NumberFormat acc. to your need.

Below is a working example:

int numberOfDecimalPlaces = 6;

BigDecimal bigDecimal = new BigDecimal(11212.122323);
bigDecimal.setScale(numberOfDecimalPlaces, RoundingMode.HALF_UP);

NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMinimumFractionDigits(numberOfDecimalPlaces);

System.out.println(numberFormat.format(bigDecimal));

Output:

11,212.122323

Java: How to set Precision for double value?

You can't set the precision of a double (or Double) to a specified number of decimal digits, because floating-point values don't have decimal digits. They have binary digits.

You will have to convert into a decimal radix, either via BigDecimal or DecimalFormat, depending on what you want to do with the value later.

See also my answer to this question for a refutation of the inevitable *100/100 answers.

Rounding Bigdecimal values with 2 Decimal Places

I think that the RoundingMode you are looking for is ROUND_HALF_EVEN. From the javadoc:

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.

Here is a quick test case:

BigDecimal a = new BigDecimal("10.12345");
BigDecimal b = new BigDecimal("10.12556");

a = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
b = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);

System.out.println(a);
System.out.println(b);

Correctly prints:

10.12
10.13

UPDATE:

setScale(int, int) has not been recommended since Java 1.5, when enums were first introduced, and was finally deprecated in Java 9. You should now use setScale(int, RoundingMode) e.g:

setScale(2, RoundingMode.HALF_EVEN)



Related Topics



Leave a reply



Submit