I was trying to make my own class for currencies using longs, but apparently I should use BigDecimal
instead. Could someone help me get started? What would be the best way to use BigDecimal
s for dollar currencies, like making it at least but no more than 2 decimal places for the cents, etc. The API for BigDecimal
is huge, and I don't know which methods to use. Also, BigDecimal
has better precision, but isn't that all lost if it passes through a double
? if I do new BigDecimal(24.99)
, how will it be different than using a double
? Or should I use the constructor that uses a String
instead?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Or, wait for JSR-354. Java Money and Currency API coming soon!
Here are a few hints:
BigDecimal
for computations if you need the precision that it offers (Money values often need this).NumberFormat
class for display. This class will take care of localization issues for amounts in different currencies. However, it will take in only primitives; therefore, if you can accept the small change in accuracy due to transformation to adouble
, you could use this class.NumberFormat
class, use thescale()
method on theBigDecimal
instance to set the precision and the rounding method.PS: In case you were wondering,
BigDecimal
is always better thandouble
, when you have to represent money values in Java.PPS:
Creating
BigDecimal
instancesThis is fairly simple since
BigDecimal
provides constructors to take in primitive values, andString
objects. You could use those, preferably the one taking theString
object. For example,Displaying
BigDecimal
instancesYou could use the
setMinimumFractionDigits
andsetMaximumFractionDigits
method calls to restrict the amount of data being displayed.I would recommend a little research on Money Pattern. Martin Fowler in his book Analysis pattern has covered this in more detail.
Coming to the usage:
You would represent all monies using
Money
object as opposed toBigDecimal
. Representing money as big decimal will mean that you will have the to format the money every where you display it. Just imagine if the display standard changes. You will have to make the edits all over the place. Instead using theMoney
pattern you centralize the formatting of money to a single location.1) If you are limited to the
double
precision, one reason to useBigDecimal
s is to realize operations with theBigDecimal
s created from thedouble
s.2) The
BigDecimal
consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, while the double wraps a value of the primitive typedouble
in an object. An object of typeDouble
contains a single field whose type isdouble
3) It should make no difference
You should have no difficulties with the $ and precision. One way to do it is using
System.out.printf
I would be radical. No BigDecimal.
Here is a great article https://lemnik.wordpress.com/2011/03/25/bigdecimal-and-your-money/
Ideas from here.
It prints
How about storing BigDecimal into a database? Hell, it also stores as a double value??? At least, if I use mongoDb without any advanced configuration it will store
BigDecimal.TEN
as1E1
.Possible solutions?
I came with one - use String to store BigDecimal in Java as a String into the database. You have validation, for example
@NotNull
,@Min(10)
, etc... Then you can use a trigger on update or save to check if current string is a number you need. There are no triggers for mongo though. Is there a built-in way for Mongodb trigger function calls?There is one drawback I am having fun around - BigDecimal as String in Swagger defenition
I need to generate swagger, so our front-end team understands that I pass them a number presented as a String. DateTime for example presented as a String.
There is another cool solution I read in the article above... Use long to store precise numbers.
Update
https://stackoverflow.com/a/27978223/4587961
Maybe in the future MongoDb will add support for BigDecimal. https://jira.mongodb.org/browse/SERVER-1393 3.3.8 seems to have this done.
It is an example of the second approach. Use scaling. http://www.technology-ebay.de/the-teams/mobile-de/blog/mapping-bigdecimals-with-morphia-for-mongodb.html