You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-5Lines changed: 21 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ What’s the issue? Why aren’t JS Numbers good enough? In what sense are they
23
23
24
24
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).
25
25
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 ergonomicsand 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.
27
27
28
28
### Primary use case: Representing human-readable decimal values such as money
Let's convert USD to EUR, given the exchange rate EUR --> USD.
65
65
66
66
```js
67
-
let exchangeRateEurToUsd =newDecimal("1.09");
68
-
let amountInUsd =newDecimal("450.27");
67
+
let exchangeRateEurToUsd =newDecimal(1.09);
68
+
let amountInUsd =newDecimal(450.27);
69
69
let exchangeRateUsdToEur =newDecimal(1).divide(exchangeRateEurToUsd);
70
70
71
71
let amountInEur =exchangeRateUsdToEur.multiply(amountInUsd);
72
72
console.log(amountInEur.round(2).toString());
73
73
```
74
74
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 =newDecimal(1.09);
80
+
let amountInUsd =newDecimal(450.27);
81
+
let exchangeRateUsdToEur =newDecimal(1).divide(exchangeRateEurToUsd);
82
+
let amountInEur =exchangeRateUsdToEur.multiply(amountInUsd);
83
+
let opts = { style:"currency", currency:"EUR" };
84
+
let formatter =newIntl.NumberFormat("en-US", opts);
85
+
let amount =Decimal.Amount(amountInEur).with({ fractionDigits:2 });
86
+
console.log(formatter.format(amount));
87
+
```
88
+
75
89
##### Format decimals with Intl.NumberFormat
76
90
77
91
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.
78
92
79
93
```js
80
-
let a =newDecimal.Amount("1.90", 4); // 4 fractional digits
94
+
let a =Decimal.Amount.from("1.90").with({ fractionDigits:4});
81
95
constformatter=newIntl.NumberFormat("de-DE");
82
96
formatter.format(a); // "1,9000"
83
97
```
@@ -134,6 +148,8 @@ Interaction with other systems brings the following requirements:
134
148
- Serialization and deserialization in standard decimal formats, e.g., IEEE 754’s multiple formats
135
149
- Precision sufficient for the applications on the other side
136
150
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
+
137
153
#### Sample code
138
154
139
155
##### 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
191
207
192
208
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.
193
209
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.
0 commit comments