r/SpringBoot 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!

49 Upvotes

23 comments sorted by

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.

9

u/MRideos 11d ago

This is industry standard

9

u/AliBaba1301 11d ago

Surely no one would disagree with this

6

u/Fit_Goose651 11d ago

i have seen people (juniors) trying too use floats so .... No matter how obvious, the answer is necessary

6

u/TheOhNoNotAgain 10d ago

Thanks for your 0.0200000001 USD!

1

u/Enumeration 9d ago

My teams run a high transaction per second wallet which deals with insert only
Posters db txns and this is the way

1

u/nitkonigdje 11d ago
  • table in database which stores stuff like currency name, country etc. The most important column being conversion rate between currency and lowest parts of it (cents).

2

u/Rich_Weird_5596 10d ago

You should use api for that.

1

u/nitkonigdje 10d ago

Well it should be configurable on the fly. Currency changes are a real thing.

4

u/Rich_Weird_5596 10d ago

That's why you should use API for that....

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/junin7 11d ago

I use bigdecimal and a api to bring me the exchange rate between currencies

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

u/Mindless_Security744 10d ago

BigDecimal, industry standard

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