Index

no-floating-decimal

禁止数字字面中的前导或尾随小数点

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

JavaScript 中的浮点值包含小数点,并且小数点前后没有数字的要求。例如,以下都是有效的 JavaScript 数字:

🌐 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:

var num = .5;
var num = 2.;
var num = -.7;

虽然这不是语法错误,但这种数字格式可能会让人难以区分真正的小数和点运算符。因此,有人建议你应该始终在小数点的前后都写上数字,以明确其意图是创建一个小数。

🌐 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.

规则详情

🌐 Rule Details

此规则旨在消除浮动小数点,并且每当数值有小数点但在其前后缺少数字时都会触发警告。

🌐 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.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-floating-decimal: "error"*/

var num = .5;
var num = 2.;
var num = -.7;

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-floating-decimal: "error"*/

var num = 0.5;
var num = 2.0;
var num = -0.7;

何时不使用

🌐 When Not To Use It

如果你不担心会误解浮点小数点值,那么你可以安全地关闭此规则。

🌐 If you aren’t concerned about misinterpreting floating decimal point values, then you can safely turn this rule off.

兼容性

🌐 Compatibility

  • JSHint: W008,W047

版本

此规则是在 ESLint v0.0.6 中引入。

资源