]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-loss-of-precision.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-loss-of-precision.md
1 ---
2 title: no-loss-of-precision
3 layout: doc
4 rule_type: problem
5 ---
6
7
8
9 This rule would disallow the use of number literals that lose precision at runtime when converted to a JS `Number` due to 64-bit floating-point rounding.
10
11 ## Rule Details
12
13 In JS, `Number`s are stored as double-precision floating-point numbers according to the [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754). Because of this, numbers can only retain accuracy up to a certain amount of digits. If the programmer enters additional digits, those digits will be lost in the conversion to the `Number` type and will result in unexpected behavior.
14
15 Examples of **incorrect** code for this rule:
16
17 ::: incorrect
18
19 ```js
20 /*eslint no-loss-of-precision: "error"*/
21
22 const x = 9007199254740993
23 const x = 5123000000000000000000000000001
24 const x = 1230000000000000000000000.0
25 const x = .1230000000000000000000000
26 const x = 0X20000000000001
27 const x = 0X2_000000000_0001;
28 ```
29
30 :::
31
32 Examples of **correct** code for this rule:
33
34 ::: correct
35
36 ```js
37 /*eslint no-loss-of-precision: "error"*/
38
39 const x = 12345
40 const x = 123.456
41 const x = 123e34
42 const x = 12300000000000000000000000
43 const x = 0x1FFFFFFFFFFFFF
44 const x = 9007199254740991
45 const x = 9007_1992547409_91
46 ```
47
48 :::