Skip to content

Add Decimal.Amount example and remove discussion of performance#195

Merged
jessealama merged 3 commits intomainfrom
may-2025-plenary-minisummit
Jun 11, 2025
Merged

Add Decimal.Amount example and remove discussion of performance#195
jessealama merged 3 commits intomainfrom
may-2025-plenary-minisummit

Conversation

@jessealama
Copy link
Copy Markdown
Collaborator

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

Comment thread README.md Outdated
Comment thread README.md
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread README.md Outdated
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
Copy Markdown

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.

@jessealama jessealama merged commit 57237cd into main Jun 11, 2025
1 check passed
@jessealama jessealama deleted the may-2025-plenary-minisummit branch June 11, 2025 08:06
jessealama added a commit that referenced this pull request Jul 3, 2025
* Add `Decimal.Amount` example and remove discussion of performance

* Update README.md

---------

Co-authored-by: Eemeli Aro <eemeli@gmail.com>
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