Use of java.math.MathContext

Recently I attempted recognizing making use of java.math.MathContext yet fell short to recognize effectively. Is it made use of for rounding in java.math.BigDecimal. If yes why does not it round the decimal figures yet also mantissa component.

From API docs, I familiarized that it adheres to the typical defined in ANSI X3.274-1996 and also ANSI X3.274-1996/AM 1-2000 requirements yet I did not get them to read online.

Please allow me recognize if you have any kind of suggestion on this.

0
2019-05-18 22:11:24
Source Share
Answers: 3

If I'm recognizing you appropriately, it seems like you are anticipating the MathContext to regulate the amount of figures need to be maintained after the decimal factor. That is not what it is for. It defines the amount of figures to maintain, complete . So if you define that you desire 3 substantial figures, that is all you are going to get.

As an example, this:

System.out.println(new BigDecimal("1234567890.123456789",
                   new MathContext(20)));

System.out.println(new BigDecimal("1234567890.123456789",
                   new MathContext(10)));

System.out.println(new BigDecimal("1234567890.123456789",
                   new MathContext(5)));

will certainly result:

1234567890.123456789
1234567890
1.2346E+9
0
2019-05-21 06:23:23
Source

@jatan

Thanks for you address. It makes good sense. Can you please clarify me MathContext in the context of BigDecimal #round method.

There is second best concerning BigDecimal.round() vs. any kind of various other BigDecimal method. In all instances, the MathContext defines the variety of substantial figures and also the rounding strategy. Primarily, there are 2 components of every MathContext. There is an accuracy, and also there is additionally a RoundingMode.

The accuracy once more defines the variety of substantial figures. So if you define 123 as a number, and also request for 2 substantial figures, you are going to get 120. It could be more clear if you assume in regards to clinical symbols.

123 would certainly be 1.23e2 in clinical symbols. If you just maintain 2 substantial figures, after that you get 1.2e2, or 120. By lowering the variety of substantial figures, we lower the accuracy with which we can define a number.

The RoundingMode component defines just how we need to take care of the loss of accuracy. To recycle the instance, if you make use of 123 as the number, and also request for 2 substantial figures, you've lowered your accuracy. With a RoundingMode of HALF_UP (the default setting), 123 will certainly come to be 120. With a RoundingMode of CEILING, you'll get 130.

As an example:

System.out.println(new BigDecimal("123.4",
                   new MathContext(4,RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("123.4",
                   new MathContext(2,RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("123.4",
                   new MathContext(2,RoundingMode.CEILING)));
System.out.println(new BigDecimal("123.4",
                   new MathContext(1,RoundingMode.CEILING)));

Outputs:

123.4
1.2E+2
1.3E+2
2E+2

You can see that both the accuracy and also the rounding setting influence the result.

0
2019-05-21 06:18:29
Source

It is except enjoyable. In fact I located some on-line instance, which mentioned making use of MathContext to round the amounts/numbers saved in BigDecimal.

As an example,

If MathContext is set up to have precision = 2 and also rounding mode = ROUND_HALF_EVEN

BigDecimal Number = 0.5294, is rounded to 0.53

So I assumed it is a more recent strategy and also utilized it for rounding objective. Nonetheless it developed into problem due to the fact that it began rounding also mentissa component of number.

As an example,

Number = 1.5294 is rounded to 1.5

Number = 10.5294 is rounded to 10

Number = 101.5294 is rounded to 100

and more

So this is not the actions I anticipated for rounding (as accuracy = 2).

It appears to be having some reasoning due to the fact that from line of gab I can claim that it takes first 2 figures (as accuracy is 2) of number and afterwards adds 0 is till the no. of figures come to be like unrounded quantity (check out the instance of 101.5294 ...)

0
2019-05-21 05:53:04
Source