r/java • • Jul 23 '26

JEP 540: Simple JSON API (Incubator)

https://openjdk.org/jeps/540
79 Upvotes

81 comments sorted by

View all comments

Show parent comment

1

u/gnahraf Jul 24 '26

You're right. The common workaround is to express such fixed precision data in their smallest unit, for eg, in cents instead of dollars. For other units, it's not so clear (eg units of weight).

You can also model fixed decimals using a long (unscaled value) and a byte (for where the decimal point goes). Like a lightweight, but fixed-range, BD. But, ofc, this would be clumsy in json.

3

u/ThinkPreparation8975 Jul 24 '26

Don't forget about unitless numbers, of which there are plenty - rate, percent, factor, even ratio (how do you express 1/3?).

And why would you suffer with modeling decimals with a long and a byte yourself, when the Java library has already solved this problem for you?

BigDecimals are ubiquitous in most financial circles. I don't know about HFT, but for banks' middle- and backoffice they are the obvious choice.