• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BigDecimal Big Troubles

 
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assisting a developer.   It's not my code so I don't know the entire story behind it.
This is for monetary amounts.   I googled around a while and see posts about BigDecimal not being accurate at times with large numbers.   I'm wondering if it has to do with the idea that there is not a way to perfectly imitate a number such as .1 in binary.   I took some wild and probably useless guesses below.   I'm looking for a way to append these numbers.  I don't know if append means add or attach the left and right side of a decimal number or something else.
Thanks,
Kevin



Maybe remove the BigDecimal in front although it is a method belonging to BigDecimal so I don't think it makes a difference.

 

Maybe do the setScale to two the two parts separately and take a look at them. Do the append after.

Maybe also do a setScale on the rs before the append.

There is a SetScale that has a third parameter. I don't know what it does.
 
Marshal
Posts: 4514
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I googled around a while and see posts about BigDecimal not being accurate at times with large numbers.


Please share links to the posts.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron McLead...Here is one place where I see HALF_ROUND_UP is depricated:

https://stackoverflow.com/questions/47268003/deprecated-constructor-for-bigdecimal-setscaleint-int-and-roundingmode-enums
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is on CodeRanch:

https://coderanch.com/t/406596/java/BigDecimal-setScale
 
Master Rancher
Posts: 4831
74
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Ron McLead...Here is one place where I see HALF_ROUND_UP is depricated:

https://stackoverflow.com/questions/47268003/deprecated-constructor-for-bigdecimal-setscaleint-int-and-roundingmode-enums


They deprecated some methods and added others to replace them, to make the API clearer.  Don't use BigDecimal.ROUND_HALF_UP, instead use RoundingMode.HALF_UP.  It's clear in the documentation.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is on CodeRanch:

https://coderanch.com/t/406596/java/BigDecimal-setScale
 
Mike Simmons
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the same one you already posted, right?  Reading it, it seems the poster thought that "down" for a negative number, would mean more negative.  Instead, the documentation shows that "down" means "towards zero", so -0.005 rounded to 0.00 rather than -0.01.  The rounding does what the documentation says it does.  If you want something different, more work may be required.
 
Mike Simmons
Master Rancher
Posts: 4831
74
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aside from those posts, the most common problem I see for people using BigDecimal is that if they create the BigDecimal from a double, they often may be creating a BigDecimal that already has an imprecise value build into it.  Try to create BigDecimals using strings or integers - if you use doubles, it may already be too late.
 
Marshal
Posts: 79249
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . I see HALF_ROUND_UP is depricated . . .

Yes, because the int fields have been superseded by the enum mentioned. If you are rounding and setting the scale simultaneously, find out about MathContext.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:This is on CodeRanch:

https://coderanch.com/t/406596/java/BigDecimal-setScale



Campbell,
The documentation link is not working for me.  I want to go there to take a look.  
Thanks,
Kevin
 
Mike Simmons
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you meant in my post, on the word "documentation".  I don't know what happened to that link, it went to a different search engine.  Anyway, I have fixed it above; also, here it is as plain text: https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/math/BigDecimal.html#ROUND_HALF_UP
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

Thank you for fixing the link.  I don't need to use this now, but if I do a search on the topic later on, I can get to the document.

Best,

Kevin
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic