Skip to content

Commit e6687b1

Browse files
committed
Add Decimal.Amount example and remove discussion of performance
1 parent 7cd95e0 commit e6687b1

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ What’s the issue? Why aren’t JS Numbers good enough? In what sense are they
2323

2424
As currently defined in JavaScript, Numbers are 64-bit binary floating-point numbers. The conversion from most decimal values to binary floats rarely is an exact match. For instance: the decimal number 0.5 can be exactly represented in binary, but not 0.1; in fact, the the 64-bit floating point number corresponding to 0.1 is actually 0.1000000000000000055511151231257827021181583404541015625. Same for 0.2, 0.3, … Statistically, most human-authored decimal numbers cannot be exactly represented as a binary floating-point number (AKA float).
2525

26-
The goal of the Decimal proposal is to add support to the JavaScript standard library for decimal numbers in a way that provides good ergonomics, functionality, and performance. JS programmers should feel comfortable using decimal numbers, when that’s appropriate. Being built-in to JavaScript means that we will get optimizable, well-maintained implementations that don’t require transmitting, storing, or parsing and jit-optimizing every additional JavaScript code.
26+
The goal of the Decimal proposal is to add support to the JavaScript standard library for decimal numbers in a way that provides good ergonomics and functionality. JS programmers should feel comfortable using decimal numbers, when those are appropriate. Being built-in to JavaScript means that we will get optimizable, well-maintained implementations that don’t require transmitting, storing, or parsing and jit-optimizing every additional JavaScript code.
2727

2828
### Primary use case: Representing human-readable decimal values such as money
2929

@@ -64,20 +64,34 @@ console.log(total.toFixed(2));
6464
Let's convert USD to EUR, given the exchange rate EUR --> USD.
6565

6666
```js
67-
let exchangeRateEurToUsd = new Decimal("1.09");
68-
let amountInUsd = new Decimal("450.27");
67+
let exchangeRateEurToUsd = new Decimal(1.09);
68+
let amountInUsd = new Decimal(450.27);
6969
let exchangeRateUsdToEur = new Decimal(1).divide(exchangeRateEurToUsd);
7070

7171
let amountInEur = exchangeRateUsdToEur.multiply(amountInUsd);
7272
console.log(amountInEur.round(2).toString());
7373
```
7474

75+
Here's the same example, this time using `Decimal.Amount`
76+
and `Intl.NumberFormat` to properly handle currency:
77+
78+
```js
79+
let exchangeRateEurToUsd = new Decimal(1.09);
80+
let amountInUsd = new Decimal(450.27);
81+
let exchangeRateUsdToEur = new Decimal(1).divide(exchangeRateEurToUsd);
82+
let amountInEur = exchangeRateUsdToEur.multiply(amountInUsd);
83+
let opts = { style: "currency", currency: "USD" };
84+
let formatter = new Intl.NumberFormat("en-US", opts);
85+
let amount = Decimal.Amount(amountInEur).with({ fractionDigits: 2 });
86+
console.log(formatter.format(amount));
87+
```
88+
7589
##### Format decimals with Intl.NumberFormat
7690

7791
We propose a `Decimal.Amount` object to store a Decimal value together with precision information. This is especially useful in formatting Decimal values, especially in internationalization and localization contexts.
7892

7993
```js
80-
let a = new Decimal.Amount("1.90", 4); // 4 fractional digits
94+
let a = Decimal.Amount.from("1.90").with({ fractionDigit: 4 });
8195
const formatter = new Intl.NumberFormat("de-DE");
8296
formatter.format(a); // "1,9000"
8397
```
@@ -134,6 +148,8 @@ Interaction with other systems brings the following requirements:
134148
- Serialization and deserialization in standard decimal formats, e.g., IEEE 754’s multiple formats
135149
- Precision sufficient for the applications on the other side
136150

151+
The `Decimal.Amount` class also helps with data exchange, in cases where one needs to preserve all digits—including any trailing zeroes—in a digit string coming over the wire. That is, the `Decimal.Amount` class contains more information that a mathematical value.
152+
137153
#### Sample code
138154

139155
##### Configure a database adapter to use JS-native decimals
@@ -191,7 +207,7 @@ Based on feedback from JS developers, engine implementors, and the members of th
191207

192208
We will use the **Decimal128** data model for JavaScript decimals. Decimal128 is not a new standard; it was added to the IEEE 754 floating-point arithmetic standard in 2008. It represents the culmination of decades of research, both theoretical and practical, on decimal floating-point numbers. Values in the Decimal128 universe take up 128 bits. In this representation, up to 34 significant digits (that is, decimal digits) can be stored, with an exponent (power of ten) of +/- 6143.
193209

194-
In addition to proposing a new `Decimal` class, we propose a `Decimal.Amount` class for storing a Decimal number together with a precision (i.e., number of significant digits). The second class is important mainly for string formatting purposes, where one desires to have a notion of a number that “knows” how precise it is. The `Decimal.Amount` class also helps with data exchange, in cases where one needs to preserve all digits—including any trailing zeroes—in a digit string coming over the wire. That is, the `Decimal.Amount` class contains more information that a mathematical value.
210+
In addition to proposing a new `Decimal` class, we propose a `Decimal.Amount` class for storing a Decimal number together with a precision. The `Decimal.Amount` class is important mainly for string formatting purposes, where one desires to have a notion of a number that “knows” how precise it is. We do not intend to support arithmetic on `Decimal.Amount` values, the thinking being that `Decimal` already supports arithmetic.
195211

196212
### Known alternatives
197213

0 commit comments

Comments
 (0)