Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Big Decimal numbers subtraction is not working

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing simple issue in below program. In below code I am just subtracting numbers and expected output is "89.50" but it is printing 90. one more thing is 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.

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is most likely to do with your MathContext

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.


I'll see if I can get this working and let you know.
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


In that case I'll stop trying to get a working example.
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pete,

I would like to learn from mistakes. Can you let me know what is the problem here.

Thanks,
Raghu
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I would like to know why subtraction is not working second time.

Thanks,
Raghu
 
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be clear about what you're asking. You posted code, so reference the line number. Which line do you mean when you refer to the "second time". Also, ItDoesntWorkIsUseless (←click that link) -- tell us what you mean by "It's not working" - what did you expect and what did you actually get? (Edit: Ok, I see you said you were getting "90" printed out when you expected "89.50")

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.
 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


How did you come to this conclusion? Just because it might be displayed as "120.0" doesn't mean it's stored like that. If you want to display it with a specific number of decimal places, use a format string:

Try this:

Output of that code will be:

120.0
120.00
[120.000000] [120.0] [   120.000]
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghu kalachar wrote:Pete,

I would like to learn from mistakes. Can you let me know what is the problem here.


Currently I do not know too much about the BigDecimal class.
As a result I was searching for something that works and then I was trying to incorporate that into your sample code.

One place to start when things do not go as plan is to look at the API documentation.
When I did that I came across the probable reason for the problem to be the MathContext object/class.
However I do not know the answer.

It appears as though Junilu Lacar has an answer:

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.


So, while I was not able to totally solve the problem I was at least able to narrow down the reason for the problem.
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu & Pete,

I have modified code to use new constructor for MathContext. I have expeted output of code to be 89.50 but it is 89.5 only. Anything else I need to do here

 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghu kalachar wrote:I have expeted output of code to be 89.50 but it is 89.5 only.


That is an output formatting issue, nothing to do with the MathContext or the BigDecimal class. See my previous reply regarding the use of format strings.
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it Junilu. It is scaling issue. Here is working code. Thanks you all for your support guys.

 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghu kalachar wrote:It is scaling issue. Here is working code.


No it's not a scaling issue. The "solution" you found has the side-effect of changing the result of the BigDecimal.toString() method. However, the intent of setScale() is to fine-tune the results of internal calculations performed on the BigDecimal value. If you only want the scale of your BigDecimal to be 2 because that's all you need for calculation results to be acceptable, that's fine, set the scale. But if you're using setScale() so that your output will show "120.00" instead of "120.0," then you're using the wrong approach and doing it for the wrong reasons, IMO.
 
Campbell Ritchie
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghu kalachar wrote:. . . I have modified code to use new constructor for MathContext.

You modified two things, both the precision and the rounding mode. Change things one at a time when investigating code problems. You shou‍ld have modified the proecison alone, and you would still have got 89.5 displayed.
 
Campbell Ritchie
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the subtract and toString methods of BigDecimal, you will see they depend on the scale. I was a bit confused, well, you have all sorts of things which are acting as red herrings. You shou‍ld have printed the results of subtracting 30 from 120 first, and you would have seen no figures after the decimal point. I have realised you have the problem of using the wrong constructor for BigDecimal. You are passing 120.00, but neither the constructor nor the valueOf() method will record the .00 part of the number. So you are doing arithmetic with the equivalent of 120 and 30, without any figures after the decimal point.
If you need the scale of 2, change all your instance creations to use the BigDecimal(String) constructor. That will retain the .00 part, and you will then get the scale of 2 you have been looking for.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic