https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html wrote:subtract(BigDecimal subtrahend, MathContext mc)
Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.
“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
Campbell Ritchie wrote:PL is correct; you have misread the details of MathContext. The 2 means significant figures, not places after the decimal point.
Please search my posts for things about big decimal: I think this post and this one will be helpful to you.
“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
raghu kalachar wrote:120.00 is getting stored in memory as 120.0. May I know the reason for both output and the way number (120.00) getting stored and help me with code to get expected output.
raghu kalachar wrote:Pete,
I would like to learn from mistakes. Can you let me know what is the problem here.
Junilu Lacar wrote:If you want to see your code work, change the argument to your MathContext constructor on line 6 to a bigger value, like 32 or even just 5.
“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
raghu kalachar wrote:I have expeted output of code to be 89.50 but it is 89.5 only.
raghu kalachar wrote:It is scaling issue. Here is working code.
You modified two things, both the precision and the rounding mode. Change things one at a time when investigating code problems. You should have modified the proecison alone, and you would still have got 89.5 displayed.raghu kalachar wrote:. . . I have modified code to use new constructor for MathContext.
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|