News for hackers. Stuff that matters.
Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @10:31AM (#48698623)
> thank you Rust decimals represented as JSON floats

What do you mean? JSON doesn’t have floats, it has numbers, and how they’re used after being parsed is not part of the spec.

> If I ever see a monetary value stored in something else than integers I'm going to run away screaming

That’s good, then we’ll likely not be working on the same system :) I consider running from “amounts as integer” systems these days (but usually unfortunately can’t). In an idealized codebase that only seasoned financial programmers are allowed to touch, it can go well, but such a system is usually either overly exclusive or risks becoming brittle.

Re:Fintech Engineering Handbook by zdragnar on Saturday June 27, 2026 @11:51AM (#48699294)
> JSON doesn’t have floats, it has numbers, and how they’re used after being parsed is not part of the spec.

I think that's the problem they were trying to describe. Without a formal spec, systems won't agree on how to handle floats. JS engines treat numbers as 53 bit signed floats, so passing a well defined decimal there through JSON means losing precision at the edges.

Money stored in integers gets around the issue by simple virtue of not really needing more than 53 bits to accurately represent the values anyone is going to encounter.

There are downsides like all the extra math or functions to handle doing the math everywhere money is manipulated or displayed, but this is the sort of thing where static typing is really helpful, and isn't too hard for juniors to understand that they should always use money functions to work with money data.

Re:Fintech Engineering Handbook by dotancohen on Saturday June 27, 2026 @04:16PM (#48701339)
With I need to represent monetary amounts through JSON, I encode it as a base 10 string and wrap it in quotes so that the JavaScript engines treat it as a string. Conversion back to int happens after the string has been parsed and validated.

I do this with HTTP GET and POST form requests as well. In HTTP, everything is a string (even if that string is JSON).

Re:Fintech Engineering Handbook by worik on Saturday June 27, 2026 @06:57PM (#48702582)
> JS engines treat numbers as 53 bit signed floats

Please do not use Javascript for finance applications. Just do not do it

Save it for user interface elements and dancing whizzimagigs, where it will do less harm.

Re:Fintech Engineering Handbook by zdragnar on Saturday June 27, 2026 @07:24PM (#48702792)
Excel does the same thing, and has been used to track money for decades. Somehow, we're all still alive.
Re:Fintech Engineering Handbook by lxgr on Sunday June 28, 2026 @02:35PM (#48710117)
Financial applications need user interface elements too, and doing these poorly has caused quite some [1] harm in the past...

[1] https://financialregulation.linklaters.com/post/102j8af/the-...

Re:Fintech Engineering Handbook by morpheuskafka on Saturday June 27, 2026 @10:48AM (#48698779)
I'm sure there is something I don't know here, but how is working with integers "brittle"? The only issue I see is rounding down by default, not sure if that is even an issue or not. At any rate, it seems a lot less brittle than floats or bigdecimal style number classes.
Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @10:56AM (#48698840)
The brittleness comes from the fact that the number of implied decimal digits per currency isn't always well-defined across all stakeholders and systems.

If you're only working in a single currency, there's usually no issue.

Re:Fintech Engineering Handbook by foresterre on Saturday June 27, 2026 @11:21AM (#48699035)
As a general rule, you always include the currency code (EUR, SEK, USD etc.) and if possible also the amount of decimals, when using minor units.

Currency codes can be found in ISO 4217.

Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @11:24AM (#48699059)
Yes, definitely always include the number of digits, but at your system boundary you still have to pray that whoever you're working with isn't silently dropping that number and re-deriving it from their own, almost-4217-compliant currency database.

Redundancy can be great, but it's not a panacea, since it's not guaranteed to be used in an optimal way.

Re:Fintech Engineering Handbook by jyounker on Saturday June 27, 2026 @04:38PM (#48701539)
How you expose you present your data to third parties is not how you represent it internally.
Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @06:03PM (#48702199)
Ideally it’s not, but I’ve seen it leak so often that I’d rather be safe than sorry.
Re:Fintech Engineering Handbook by 0xffff2 on Saturday June 27, 2026 @01:48PM (#48700218)
ISO 4217 also defines the number of significant digits after the decimal separator if Wikipedia is to be believed.
Re:Fintech Engineering Handbook by lawlorino on Saturday June 27, 2026 @10:44AM (#48698740)
> I consider running from “amounts as integer” systems these days (but usually unfortunately can’t).

In the context of Fintech, how do you otherwise resolve floating point rounding issues if not representing amounts with integers?

Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @10:57AM (#48698855)
Native decimal types, if your system has them. Many languages and databases used in financial contexts do.
Re:Fintech Engineering Handbook by Maxatar on Saturday June 27, 2026 @01:37PM (#48700123)
But native decimal libraries are almost always floating point.

Do people not know what a floating point number is?

Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @02:44PM (#48700638)
That’s alright, the important part here is that they’re decimal, not binary. You don’t want 0.1 + 0.2 to equal 0.300…004.
Re:Fintech Engineering Handbook by Maxatar on Saturday June 27, 2026 @03:56PM (#48701171)
There are pretty trivial ways to use binary floating point values that don't result in 0.1 + 0.2 producing 0.30000...4 and it saddens me when this topic comes up and people go to such extreme lengths to recreate a second hand buggy reimplementation of a subset of floating point numbers to do it.
Re:Fintech Engineering Handbook by worik on Saturday June 27, 2026 @07:01PM (#48702607)
> There are pretty trivial ways to use binary floating point values that don't result in 0.1 + 0.2 producing 0.30000...4

Not across all architectures and operating systems there are not

Listen to those who have done this. Use integers for finance.

Re:Fintech Engineering Handbook by Maxatar on Saturday June 27, 2026 @08:40PM (#48703217)
Yes across all architectures and OS's that use IEEE 754 floating points.
Re:Fintech Engineering Handbook by FabHK on Sunday June 28, 2026 @11:16AM (#48708083)
What are those pretty trivial ways? And how are they better than storing 10 cents + 20 cents = 30 cents?
Re:Fintech Engineering Handbook by Maxatar on Sunday June 28, 2026 @01:53PM (#48709700)
By not thinking in terms of strict rules and dogmas and instead focusing on the actual problem you want to solve.

If you want to guarantee that adding cents together results in an exact value without any loss of precision, and you also want a tiny memory footprint and very high performance, then use a binary floating point to represent cents, just as you would use an int.

Also the question of how it's better depends on your use case and my argument is not that one representation is universally better than another, it's that money is used for such a diverse range of use cases that you need to actually understand what you're doing, what the goal is, what the potential issues are etc... in order to pick the right representation for your use case. At my trading firm we have three different Money classes optimized for three different use-cases (with functions to allow interoperability between them).

For the use case where the representation needs to use little memory, is fast, and needs to be used to perform complex financial calculations at an enormous scale, then you use binary floating point where a value of 1.0d = 0.000001 dollars. This gives you exact precision when working with cents, and lets you perform all of the usual financial computations that most quantitative applications need to perform with excellent performance.

We also have a use case optimized for I/O, where no calculations are expected to be performed but the data will be transmitted over a network or to/from a database etc...

If this isn't your domain, maybe you're just writing a GUI application/web app, then by all means use a big decimal or use an integer... I don't know, but it's very sad seeing how many people who are presumably professionals don't take just the bare minimum amount of time to think things through and instead just reason in terms of strict dogmas.

Re:Fintech Engineering Handbook by lxgr on Sunday June 28, 2026 @02:39PM (#48710151)
> you use binary floating point where a value of 1.0d = 0.000001 dollars

Is that actually better than just defining one dollar as 1.0? In my (pretty limited) understanding of floats, this doesn't seem to add any precision or prevent any edge cases.

Re:Fintech Engineering Handbook by Maxatar on Sunday June 28, 2026 @03:06PM (#48710423)
Depends on your use case, once again. If your use case requires that arithmetic involving cents is exact, then yes defining 1.0d = 1 cent guarantees that, just as using the int value 1 = 1 cent guarantees that. Floating point numbers can represent and perform exact arithmetic on integer values, no loss of precision.

The original example was being concerned that 10 cents plus 20 cents should equal 30 cents exactly, not 30.000000...2 cents. Well if you set 1.0d = 1 cent, you get that exact result. In fact for any arithmetic performed on values greater than or equal to 1 cent, you are guaranteed to get exact results just like you get exact results using an int to represent cents.

Now if your use case does not require exact arithmetic on cents, then by all means stick to using 1.0d = 1 dollar. Evaluate your use case and decide on an appropriate scale to use by default.

Where the benefit of floating point kicks in, for certain use cases, is that you can continue to use the same representation with operations that don't work well with fixed precision, like interest rate calculations, financial derivatives, or more advanced financial modelling techniques and you get these operations at significant orders of magnitude faster than you would using decimal representations.

And I'm not talking like 20% faster or 50% faster... the difference between using a binary floating point representation versus decimal ranges from 10x to 100x, and the difference between using a double versus a big decimal is on the order of 1000x.

In order words, a financial report that can be generated in 1 second using binary floats can end up taking almost 2 minutes using a decimal representation, and 16 minutes using a big decimal.

Re:Fintech Engineering Handbook by ktimespi on Saturday June 27, 2026 @06:57PM (#48702583)
Aren't decimal types BCD coded?
Re:Fintech Engineering Handbook by simpsond on Saturday June 27, 2026 @11:41AM (#48699212)
An integer for the value (scaled by number of decimals) and an integer value for the number of decimals. Different systems may use different values, even for the same currency or asset.
Re:Fintech Engineering Handbook by Maxatar on Saturday June 27, 2026 @01:36PM (#48700110)
What exactly do you think a floating point number is?
Re:Fintech Engineering Handbook by lxgr on Saturday June 27, 2026 @06:04PM (#48702208)
Usually that, but in binary, which causes lots of headaches for financial math.
Re:Fintech Engineering Handbook by Maxatar on Saturday June 27, 2026 @08:51PM (#48703276)
For those who don't understand what they're doing, yes. But those people end up having headaches and causing trouble no matter what.
Re:Fintech Engineering Handbook by CyberDildonics on Sunday June 28, 2026 @03:59PM (#48710981)
So all the people who write financial software and avoid floats don't know what they're doing? That's would end up being just about everyone. You should teach them, you know so much more than they do.
Re:Fintech Engineering Handbook by dgunay on Saturday June 27, 2026 @06:03PM (#48702197)
All of the data we control (in the database, in our apis, etc) is integer cents. When we have to interface with a system that represents money using JSON numbers as dollars.cents, we parse or serialize it into an arbitrary precision decimal type. Hasn't been much of a problem.
Re:Fintech Engineering Handbook by foobarian on Saturday June 27, 2026 @10:22PM (#48703755)
This started out fine for us except that int32s were a bad idea and upgrading was painful
Re:Fintech Engineering Handbook by dgunay on Sunday June 28, 2026 @01:18AM (#48704606)
That's definitely valid, for b2c applications though a lot of the time you will never deal with numbers of that magnitude.