在 JavaScript 中检查一个值是浮点型还是整数
How to check if a Value is a Float in JavaScript
检查一个值是浮点数还是整数:
- 使用
Number.isInteger()
方法检查值是否为整数。 - 检查该值是否为数字类型且不是整数,或者
NaN
检查该值是否为浮点数。
// ✅ check if a value is a float function isFloat(value) { if ( typeof value === 'number' && !Number.isNaN(value) && !Number.isInteger(value) ) { return true; } return false; } console.log(isFloat(1)); // 👉️ false console.log(isFloat(1.5)); // 👉️ true console.log(isFloat(-1.5)); // 👉️ true console.log(isFloat('1.5')); // 👉️ false
要检查数字是否为浮点数,请检查该值是否具有数字类型并且不是整数或NaN
.
我们首先检查提供的值是否具有数字类型。如果没有,我们false
马上返回。
function isFloat(value) { if ( typeof value === 'number' && !Number.isNaN(value) && !Number.isInteger(value) ) { return true; } return false; }
我们使用
逻辑与 (&&)
运算符来检查多个条件。为了让我们的if
区块运行,必须满足所有条件。
第二个条件检查值是否不是NaN
(不是数字)。不幸的是,在 JavaScript 中NaN
有一个类型number
。
console.log(typeof Number.NaN); // 👉️ number
如果提供的值是类型number
,不是NaN
且不是整数,则该值是浮点数。
检查一个值是否为整数
我们使用该Number.isInteger()
方法来检查值是否为整数。
console.log(Number.isInteger(1)); // 👉️ true console.log(Number.isInteger('1')); // 👉️ false console.log(Number.isInteger(1.5)); // 👉️ false
使用Number.isInteger()方法时有一个问题
。
true
如果传入值返回:
- 是一个整数
- 是一个可以表示为整数的浮点数
Here’s an example of a float that can be represented as an integer.
console.log(Number.isInteger(10.0)); // 👉️ true
It depends on your use case whether you consider 10.0
to be an integer or
float.
Our implementation of the function considers numbers like 1.0
and 5.0
to be
integers.
A custom function that checks if the value is int or float #
You can combine the conditions into a single function to check if a value is a
float or an integer.
function isIntOrFloat(value) { if (typeof value === 'number' && !Number.isNaN(value)) { if (Number.isInteger(value)) { console.log('The value is an integer', value); } else { console.log('The value is a floating-point number', value); } } else { console.log('The value is not a number', value); } } isIntOrFloat(1); // 👉️ The value is an integer 1 isIntOrFloat(1.5); // 👉️ The value is a floating-point number 1.5 isIntOrFloat('1'); // 👉️ The value is not a number 1 isIntOrFloat(5.0); // 👉️ The value is an integer 5 isIntOrFloat('test'); // 👉️ The value is not a number test
The function first checks if the value is a number and is not NaN
because
NaN
has a type of number
in JavaScript.
If we enter the if
block, we know that the value is a number.
The next step is to check if the value is an integer using the
Number.isInteger()
function.
If the value is an integer
, the if
block runs.
else
block runs.If the initial conditional check fails and the value does not have a type of
number
or is NaN
, then the value is not a number.
Checking if the value is a valid integer using RegExp.test()
#
You can also use the RegExp.test()
method to check if a value is a valid
integer.
function isInteger(value) { return /^-?[0-9]+$/.test(value); } console.log(isInteger(12)); // 👉️ true console.log(isInteger(-12)); // 👉️ true console.log(isInteger(1.0)); // 👉️ true console.log(isInteger('1')); // 👉️ true console.log(isInteger(1.5)); // 👉️ false
The
RegExp.test
method matches the specified regular expression against the supplied value and
returns true
if the regular expression is matched in the string and false
otherwise.
The forward slashes / /
mark the beginning and end of the regular expression.
The caret ^
matches the beginning of the input and the dollar sign $
matches
the end of the input.
We used a hyphen to also allow for negative integers.
?
matches the preceding item (the minus) 0 or 1 times. In other words, the minus -
might be there, or it might not be there.The character class [0-9]
matches the digits in the range.
The plus +
matches the preceding item (the range of digits) one or more times.
If you ever need help reading a regular expression, check out this
regular expression cheatsheet
by MDN.
It contains a table with the name and the meaning of each special character with
examples.