如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
如何发现一个数字是浮点数或整数?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
当前回答
要检查数字是否为Int并应用2十进制格式,可以在React Native中使用以下公式。
isInt = (n) => {
return n % 1 === 0;
}
show = (x) => {
if(x) {
if (this.isInt(x)) {
return ${x}
}
else {
return ${x.toFixed(2)}
}
}
}
其他回答
在java脚本中,所有数字都是内部64位浮点,与java中的double相同。javascript中没有不同的类型,所有类型都由类型号表示。因此,您将无法进行检查实例。然而,你可以使用上面给出的解来确定它是否是一个分数。java脚本的设计者认为,只要使用一种类型,就可以避免大量的类型转换错误。
function isInt(n)
{
return n != "" && !isNaN(n) && Math.round(n) == n;
}
function isFloat(n){
return n != "" && !isNaN(n) && Math.round(n) != n;
}
适用于所有情况。
另一种方法是:
function isFloat(float) {
return /\./.test(float.toString());
}
可能没有其他方法有效,但另一种方法仍然有效。
使用此选项,您可以检查字符串或数字是否为“十进制”(正确浮动):
var IsDecimal = function(num){
return ((num.toString().split('.').length) <= 2 && num.toString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)) ? (!isNaN(Number.parseFloat(num))) : false ;
}
另一个用于检查字符串或数字是否为整数:
var IsInteger = function(num){
return ((num.toString().split('.').length) == 1 && num.toString().match(/^[\-]?\d+$/)) ? (!isNaN(Number.parseInt(num))) : false ;
}
var IsDecimal=函数(num){return((num.toString().split('.').length)<=2&&num.toSString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/))?(!isNaN(Number.parseFloat(num)):false;}var IsInteger=函数(num){return((num.toString().split('.').length)==1&num.toSString().match(/^[\-]?\d+$/))?(!isNaN(Number.parseInt(num)):false;}console.log(“--------------作为字符串--------------”);console.log(“整数:”);console.log(“0=”+IsInteger(“0”));console.log(“34=”+IsInteger(“34”));console.log(“.34=”+IsInteger(“.34”));console.log(“3.4=”+IsInteger(“3.4”));console.log(“3e=”+IsInteger(“3e”));console.log(“e3=”+IsInteger(“e3”));console.log(“-34=”+IsInteger(“-34”));console.log(“--34=”+IsInteger(“--34”));console.log(“034=”+IsInteger(“034”));console.log(“0-34=”+IsInteger(“0-34”));console.log(“浮点/小数:”);console.log(“0=”+IsDecimal(“0”));console.log(“64=”+IsDecimal(“64”));console.log(“.64=”+IsDecimal(“.64”));console.log(“6.4=”+IsDecimal(“6.4”));console.log(“6e2=”+IsDecimal(“6e2”));console.log(“6e=”+IsDecimal(“6e”));console.log(“e6=”+IsDecimal(“e6”));console.log(“-64=”+IsDecimal(“-64”));console.log(“--64=”+IsDecimal(“--64”));console.log(“064=”+IsDecimal(“064”));console.log(“0-64=”+IsDecimal(“0-64”));console.log(“\n--------------作为数字--------------”);console.log(“整数:”);console.log(“0=”+IsInteger(0));console.log(“34=”+IsInteger(34));console.log(“.34=”+IsInteger(0.34));console.log(“3.4=”+IsInteger(3.4));console.log(“-34=”+IsInteger(-34));console.log(“034=”+IsInteger(034));console.log(“0-34=”+IsInteger(0-34));console.log(“浮点/小数:”);console.log(“0=”+IsDecimal(0));console.log(“64=”+IsDecimal(64));console.log(“.64=”+IsDecimal(0.64));console.log(“6.4=”+IsDecimal(6.4));console.log(“6e2=”+IsDecimal(6e2));console.log(“-64=”+IsDecimal(-64));console.log(“064=”+IsDecimal(064));console.log(“0-64=”+IsDecimal(0-64));
当除以1时检查余数:
function isInt(n) {
return n % 1 === 0;
}
如果你不知道参数是一个数字,你需要两个测试:
function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
2019年更新在这个答案写出来5年后,一个解决方案在ECMA脚本2015中被标准化。这个答案涵盖了这个解决方案。