]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-loss-of-precision.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-loss-of-precision.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-loss-of-precision
8f9d1d4d
DC
3rule_type: problem
4---
ebb53d86 5
8f9d1d4d
DC
6
7
8This 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.
ebb53d86
TL
9
10## Rule Details
11
12In 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.
13
14Examples of **incorrect** code for this rule:
15
8f9d1d4d
DC
16::: incorrect
17
ebb53d86
TL
18```js
19/*eslint no-loss-of-precision: "error"*/
20
21const x = 9007199254740993
22const x = 5123000000000000000000000000001
23const x = 1230000000000000000000000.0
24const x = .1230000000000000000000000
25const x = 0X20000000000001
6f036462 26const x = 0X2_000000000_0001;
ebb53d86
TL
27```
28
8f9d1d4d
DC
29:::
30
ebb53d86
TL
31Examples of **correct** code for this rule:
32
8f9d1d4d
DC
33::: correct
34
ebb53d86
TL
35```js
36/*eslint no-loss-of-precision: "error"*/
37
38const x = 12345
39const x = 123.456
40const x = 123e34
41const x = 12300000000000000000000000
42const x = 0x1FFFFFFFFFFFFF
43const x = 9007199254740991
6f036462 44const x = 9007_1992547409_91
ebb53d86 45```
8f9d1d4d
DC
46
47:::