BigDecimal rounding standards on currency
Howdy everybody! I have been using BigDecimal for a few functions run on currency. I have used it for every intermediate operation and now need to display the final product. What is the standard for rounding? I get that it should be an easy round to 2 decimals for USD, but is the rounding strategy generally to truncate, always round up, or round even?
I have tried finding an answer online and there doesn't seem to be a legal standard or anything like that, but I would assume there is with taxes right? All input is appreciated!
9 Replies
⌛
This post has been reserved for your question.
Hey @YoungMoneyMitch! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
I think by default, it fails (exception) in case it needs rounding
but you can reconfigure that with a
MathContext
Thank you for the response!
I am trying to use
receipt.setGrandTotal(receipt.getGrandTotal().scale(2, RoundingMode.HALF_EVEN));
which uses a RoundingMode ENUM because the BigDecimal.HALF_EVEN has been depreciated. This is giving an error which makes me think I am using scale wrong. How does a MathContext fit in?
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
MathContext allows you to configure the amount of digits etc
if I recall correctly
What error are you getting?
RoundingMode
should be correctSince BigDecimal is immutable would i need to do something like
return MathContext(2, HALF_EVEN, myDecimal)
On compile
required: no arguments
found: int,RoundingMode
reason: actual and formal argument lists differ in length
but every example I can find online says use scale instead of round
MathContext mc = new MathContext(2, RoundingMode.HALF_EVEN);
receipt.setGrandTotal(receipt.getGrandTotal().round(mc));
results in
2.1E+2
but I am only wanting to round for the decimals not the total sigfigs
use setScale instead of scale?
I am dumb i need to use set scale
lol good call i just realized
Post Closed
This post has been closed by <@927297953020932186>.