I looked into using Dinero.js before but it was overkill for my use case of calculating cart totals in USD only. I went with Big.js because it was better suited for what I wanted: a small library for decimal arithmetic.
I think the advantage behind using something like Big.js is to future proof the code against hyper-inflation. Though a dedicated currency engine would better have the ability to set and conveniently update the currency denomination of stored values to cope with this. With float64's maximum 'safe' integer at almost ten thousand trillion, I believe this should be enough to compute most commercial transactions securely…
How to Handle Monetary Values in JavaScript
21–30 of 115 posts
Re: How to Handle Monetary Values in JavaScript
#22Earlier quoted context omitted.
Can you illustrate a case where you can't use regular FP arithmetic and round to 2 decimal places at the end?
You can't model 1.0 correctly but it will be something like 1.000006667. If you add enough of these numbers you will be off.
Re: How to Handle Monetary Values in JavaScript
#23Do what COBOL does and use it to verify your monetary library. Your code will agree with the majority of financial institutions.
Re: How to Handle Monetary Values in JavaScript
#24The question is how much of an issue is this really? How often is 4 parts in 10 quadrillion a meaningful error when dealing with money? Especially when most of the time JS is dealing with money it will be presentation.
Re: How to Handle Monetary Values in JavaScript
#25Re: How to Handle Monetary Values in JavaScript
#26Banking apps, e-commerce websites, stock exchange platforms, we interact with money daily. We also increasingly rely on technology to handle ours. Yet, there’s no consensus around how to programmatically handle monetary values. I wonder if the average person would find this frightening.
In the Java world, there's JSR-354: https://jcp.org/en/jsr/detail?id=354
Cobol has handled money well for decades.
.NET has Decimal, which should handle money correctly as long as you're working with values less than 79.2 octillion.
In my part of the world, at least, big banks tend to be mostly Java and Cobol on the back end. You'll definitely see Node, Go, and others being used for various tasks throughout the company, but when it comes to keeping track of money and moving it around, it's primarily Java and Cobol doing the heavy lifting.
That's what I've heard from other developers, anyway. I haven't worked at a bank myself.
Re: How to Handle Monetary Values in JavaScript
#27Yet, there’s no consensus around how to programmatically handle monetary values. Do what COBOL does and use it to verify your monetary library. Your code will agree with the majority of financial institutions.
Re: How to Handle Monetary Values in JavaScript
#28Earlier quoted context omitted.
Ever seen the numeric tower of lisp? https://en.wikipedia.org/wiki/Numerical_tower The link to the scheme documentation that goes over this is quite good.
That's fantastic! Do you have any more resources on this? I'd like to look at that concept in more depth.
Honestly, most all older manuals and documents that were not related to systems programming seem to cover numeric programming decently enough. I get the impression that languages used to worry more about that sort of thing, but then the truly heavy lifting of numeric programming moved into the specialized domains of Matlab and friends, and then programmers slowly unlearned most of what was learned regarding numbers.
Re: How to Handle Monetary Values in JavaScript
#29I really like the approach Perl 6 takes with FatRat[1]. You basically have an object which holds a numerator and denominator, so all arithmetic calculations do not loose precision. [1] https://docs.perl6.org/type/FatRat
Re: How to Handle Monetary Values in JavaScript
#30Earlier quoted context omitted.
Can you illustrate a case where you can't use regular FP arithmetic and round to 2 decimal places at the end?
Bring up a Javascript interpreter and try this: $ node > 0.1 + 0.2 - 0.3 5.551115123125783e-17 Close enough for trivial cases but errors can accumulate. Ruby, Perl5, Python also fail this. Perl6 uses Rat numbers for things like this and doesn't have this problem.
Ruby and Python have arbitrary precision decimal and rational support in the standard library. While there are arguments tobe had over whether using binary floats by default for simple decimal literals is a good choice, that's a different issue than not having any other kind of numbers available by default.