Add Decimal.Amount example and remove discussion of performance#195
Add Decimal.Amount example and remove discussion of performance#195jessealama merged 3 commits intomainfrom
Decimal.Amount example and remove discussion of performance#195Conversation
| 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.
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.
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.
(It might work with these particular values.)
There was a problem hiding this comment.
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. |
* Add `Decimal.Amount` example and remove discussion of performance * Update README.md --------- Co-authored-by: Eemeli Aro <eemeli@gmail.com>
In the May 2025 plenary we're working on polishing the README. Let's use this PR to do the work.