-
Notifications
You must be signed in to change notification settings - Fork 19
Add Decimal.Amount
example and remove discussion of performance
#195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
README.md
Outdated
Here's the same example, this time using `Decimal.Amount` | ||
and `Intl.NumberFormat` to properly handle currency: | ||
|
||
```js | ||
let exchangeRateEurToUsd = new Decimal(1.09); | ||
let amountInUsd = new Decimal(450.27); | ||
let exchangeRateUsdToEur = new Decimal(1).divide(exchangeRateEurToUsd); | ||
let amountInEur = exchangeRateUsdToEur.multiply(amountInUsd); | ||
let opts = { style: "currency", currency: "USD" }; | ||
let formatter = new Intl.NumberFormat("en-US", opts); | ||
let amount = Decimal.Amount(amountInEur).with({ fractionDigits: 2 }); | ||
console.log(formatter.format(amount)); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comparable non-Decimal and non-Amount current way of doing the same would probably be something like this:
let exchangeRateEurToUsd = 1.09;
let amountInUsd = 450.27;
let exchangeRateUsdToEur = 1 / exchangeRateEurToUsd;
let amountInEur = exchangeRateUsdToEur * amountInUsd;
let opts = { style: "currency", currency: "EUR" };
let formatter = new Intl.NumberFormat("en-US", opts);
console.log(formatter.format(amountInEur));
with exactly the same output as with Decimal/Amount. What is the positive value being proposed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thinking here is that there might be some rounding errors that happen with the computation; currency conversion is a typical example where rounding errors might surface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It might work with these particular values.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it would be best to use an example here that did show a rounding error.
Co-authored-by: Eemeli Aro <eemeli@gmail.com>
Co-authored-by: Eemeli Aro <eemeli@gmail.com>
Better. I think we can still be more poignant on the exact problem we're trying to solve. I might open a PR proposing my own wording after this one lands. |
In the May 2025 plenary we're working on polishing the README. Let's use this PR to do the work.