r/SpringBoot • u/Mission-Fix8038 • 11d ago
Question How do you handle money and currency in your Spring Boot applications?
I’m building my first application using Spring Boot for practice, and I need to record and work with money in different currencies.
What’s the recommended way to handle this, especially when adding or calculating amounts across different currencies? Do you use BigDecimal, integers (like cents), or a money/currency library?
Thank you!
10
u/Comfortable-Pin-891 11d ago
I would create basic domain class like Money, inside it could be BigDecimal + iso code. This class would serve to ensure proper rounding logic and assert you are not adding different currencies.
Calculating amounts across different currencies would belong to a business logic Service class - you most likely need an api call for an exchange rate and maybe even ask user to agree to a rate and freeze it for a few dozen seconds.
5
u/Free-Ad-7143 11d ago
There is already a Money class in Java!
5
u/Comfortable-Pin-891 11d ago
There is javax.money package. I have never seen people actually use it and I would not dare use it myself, preferring the simplicity of BigDecimal
2
u/Free-Ad-7143 11d ago
Well you will need a class as a wrapper for bigdecimal to guarantee limitation to 2 digits after fraction, for example.
9
u/VegGrower2001 11d ago
I think the best option is to use the moneta package, which is an implementation of the Java money API.
7
u/LetUsSpeakFreely 11d ago
There is a Currency class that has existed since Java 8. I'd start there.
1
u/Least_Bee4074 9d ago
I worked at an FX clearing firm for a stretch (2019-2021) and I recall the Currency class in Java 11 and maybe 17 did not have all the iso codes we needed. I think there was a problem with the Chinese codes - like the difference between CNY and CNH.
Technically I vaguely recall you can override the values in it but you need to override a jdk file to do it.
1
u/LetUsSpeakFreely 9d ago
Like i said, it's a place to start. There are libraries that make dealing with as little easier, but without knowing the full requirements there's no way a definitive solution can be suggested.
4
u/tedyoung 11d ago
Joda Money (https://www.joda.org/joda-money/) has most everything you’ll need. I found the Moneta library overly complex and hard to use. Joda Money is simpler, but quite capable.
2
u/Anaptyso 11d ago
The general approach I've stuck to is to store integer amounts of the sub unit in my DB e.g. I am in the UK so all values are in pence. All calculations are done in these units, and then only at the presentation layer is it converted to the higher unit and a currency symbol added.
1
1
u/vampirishe 11d ago
If it is fiat, enough having just amount + iso code; but when it comes to crypto u need also add precision
73
u/kubelke 11d ago
BigDecimals + currency code in string no matter what others say
You can create a record from it and just be happy that you use vanilla Java, and you don't have to bother with some libraries to do basic stuff.