Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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.



> 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.


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-...


Excel does the same thing, and has been used to track money for decades. Somehow, we're all still alive.


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).




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: