0

I need to read data from a JSON and there are some money data as shown below:

$234,205,860

I thought to map this data to my DTO class as String, but I am not sure if there is a proper data type in Java. I look at on the web, but could not see any for this kind of data.

So, is there any data type for this money value? Or should I use String to keep this kind of data in Java?

Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    I would ordinarily represent amounts of money via a fixed-point numeric data type. Details depend somewhat on the range and precision you want to represent and on the operations you need to support, but one of `long`, `int`, or `BigDecimal` would be a more conventional choice than `String`. – John Bollinger Nov 06 '22 at 23:33
  • `BigDecimal ` would be better, but I am not sure if I should parse this currency values and remove the '$' parts. Is this approach ok? Or should we keep this values as well (in case multiple currencies are stored in the same table)? – Jack Nov 06 '22 at 23:41
  • 1
    I'm sure we have no way to evaluate your project's requirements or data, other than through you. That's why I phrased my previous comment as I did, rather than giving a specific recommendation. Keeping the values as strings is fine if you will be treating them *as* opaque strings. But if you're going to want to perform arithmetic or other numeric operations with them, then I do recommend parsing them to a numeric form. And if you need to track the currency then do so -- that does not require retaining the whole thing in string form. – John Bollinger Nov 06 '22 at 23:46

2 Answers2

1

Precision is the keyword here. Double* and float are a bad choice in most cases! To not lose precision, String could come to mind and would work for storing values nicely without running into issues ruining precision. Long may be fine as well in some cases.

If you need to manipulate values in the future go for BigDecimal. What data type to use for money in Java?

Edit: *should be double (not long).

AirUp
  • 426
  • 1
  • 8
  • 18
  • `long` is fine if you understand the range and precision of the values you are storing, and decide whether to store cents or dollars. `float` and `double` are poor choices. – tgdavies Nov 07 '22 at 00:21
  • But what about the $ sign??? Should I split and remove it? – Jack Nov 07 '22 at 01:33
  • thanks tgdavies, i meant double and float should be avoided as of precision issues. @Rosa yes of course you would parse & remove the currency sign from the value itself and store it alongside for e.g. easy currency conversion in the future. – AirUp Nov 07 '22 at 08:43
1

Assuming you want to cope with multiple currencies, you should create a class that holds both the currency value and units (dollars, pounds, riyals, etc).

For the value, you could use a scaled long (i.e. store cents, or whatever the smallest unit is) and scale the numbers for input/output. That works only if you are sure the value of cents (or smallest unit) is always less than 263-1 or 9,223,372,036,854,775,807, including any intermediate results of calculations. If you don't want to worry about potential overflow, BigDecimal is the way to go.

For the units, there's a java.util.Currency class, but it may or may not meet your needs.

Why NEVER to use floating point for currency:

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190