no-floating-decimal

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

🔧 Fixable

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

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

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 中引入。

资源

ESLint 中文网
粤ICP备13048890号