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
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @YoungMoneyMitch! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2y ago
I think by default, it fails (exception) in case it needs rounding but you can reconfigure that with a MathContext
YoungMoneyMitch
YoungMoneyMitchOP2y ago
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?
JavaBot
JavaBot2y ago
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.
dan1st
dan1st2y ago
MathContext allows you to configure the amount of digits etc if I recall correctly What error are you getting? RoundingMode should be correct
YoungMoneyMitch
YoungMoneyMitchOP2y ago
Since 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
dan1st
dan1st2y ago
use setScale instead of scale?
YoungMoneyMitch
YoungMoneyMitchOP2y ago
I am dumb i need to use set scale lol good call i just realized
JavaBot
JavaBot2y ago
Post Closed
This post has been closed by <@927297953020932186>.
Want results from more Discord servers?
Add your server