• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cash Register Problem and using BigDecimal  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys! I am feeling quite victorious! I wrote this one all by myself (AND it works!!), but I know that to get really accurate results I need to use BigDecimal instead of Double. The problem is that I can't quite figure out how to do that. Can I get a little guidance on how to switch those out?

Also, was learning the vocabulary around coding easy for everyone? I pick up things fairly easilly and seem to be able to figure out how to get it to work, but understanding the terminology is still killing me.

 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I suggest you read a tutorial on using BigDecimal for currencies such as http://www.javaworld.com/article/2075315/core-java/make-cents-with-bigdecimal.html

BTW you don't have to use BigDecimal to overcome the precision errors found when using doubles, you can do all your calcs in longs with the currency value held in cents and just convert the value to dollars and cents for display purposes.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice tutorial, but a bit old. You would use rounding mode enum elements nowadays instead of the constant ints shown there.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ja Shields wrote:Hi Guys! I am feeling quite victorious! I wrote this one all by myself (AND it works!!), but I know that to get really accurate results I need to use BigDecimal instead of Double.

Well, first of all: WELL DONE.

Second: You're absolutely right, and the explanation can be found here; although some of it may go over your head just at the moment. It's a great page to have bookmarked though.

The problem is that I can't quite figure out how to do that.

Then I'd suggest trying Tony's suggestion first: use integers and deal with everything internally in pennies (or your smallest unit of currency). A long can fit more pennies than the expected GDP for the entire world in 2014, so it's unlikely that you'll run into any problems , and they'll be a LOT faster than BigDecimal's.

Also: it's extremely simple to convert to and from BigDecimal and long, so if you want to provide answers in BigDecimal form, just convert as needed.

Also, was learning the vocabulary around coding easy for everyone? I pick up things fairly easilly and seem to be able to figure out how to get it to work, but understanding the terminology is still killing me.

Join the club. I've been at this lark for 35 years, and some of it still mystifies me. I put it down to the fact that most programmers did science or engineering degrees at uni; not English.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!