Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jessealama
Copy link
Collaborator

In the May 2025 plenary we're working on polishing the README. Let's use this PR to do the work.

README.md Outdated
Comment on lines 75 to 87
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));
```
Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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

Copy link
Member

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.

jessealama and others added 2 commits May 30, 2025 11:02
Co-authored-by: Eemeli Aro <eemeli@gmail.com>
Co-authored-by: Eemeli Aro <eemeli@gmail.com>
@sffc
Copy link

sffc commented Jun 4, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants