]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-floating-decimal.md
e1c26d233b890f09c5006cf71559269b58dcd2de
[pve-eslint.git] / eslint / docs / src / rules / no-floating-decimal.md
1 ---
2 title: no-floating-decimal
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8
9 Float values in JavaScript contain a decimal point, and there is no requirement that the decimal point be preceded or followed by a number. For example, the following are all valid JavaScript numbers:
10
11 ```js
12 var num = .5;
13 var num = 2.;
14 var num = -.7;
15 ```
16
17 Although not a syntax error, this format for numbers can make it difficult to distinguish between true decimal numbers and the dot operator. For this reason, some recommend that you should always include a number before and after a decimal point to make it clear the intent is to create a decimal number.
18
19 ## Rule Details
20
21 This rule is aimed at eliminating floating decimal points and will warn whenever a numeric value has a decimal point but is missing a number either before or after it.
22
23 Examples of **incorrect** code for this rule:
24
25 ::: incorrect
26
27 ```js
28 /*eslint no-floating-decimal: "error"*/
29
30 var num = .5;
31 var num = 2.;
32 var num = -.7;
33 ```
34
35 :::
36
37 Examples of **correct** code for this rule:
38
39 ::: correct
40
41 ```js
42 /*eslint no-floating-decimal: "error"*/
43
44 var num = 0.5;
45 var num = 2.0;
46 var num = -0.7;
47 ```
48
49 :::
50
51 ## When Not To Use It
52
53 If you aren't concerned about misinterpreting floating decimal point values, then you can safely turn this rule off.
54
55 ## Compatibility
56
57 * **JSHint**: W008, W047