如何在JavaScript中将浮点数转换为整数?

JavaScript

逆天GO

2020-03-09

我想将浮点数转换为JavaScript中的整数。实际上,我想知道如何同时进行标准转换:截断和舍入。而且有效,而不是通过转换为字符串和解析。

第385篇《如何在JavaScript中将浮点数转换为整数?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

13个回答
EvaPro 2020.03.10

如果您使用的是angularjs,则简单的解决方案如下所示:HTML模板绑定

{{val | number:0}}

它将val转换为整数

使用此链接docs.angularjs.org/api/ng/filter/number

TomNear前端 2020.03.10

我只想指出,您想通过货币进行舍入,而不是舍弃。一分钱的可能性很小,因为4.999452 * 100的四舍五入将为您5提供更具代表性的答案。

最重要的是,不要忘了银行家的舍入,这是一种对付舍入产生的轻微积极偏差的方法-您的财务申请可能会要求这样做。

高斯/银行家的JavaScript四舍五入

樱Itachi 2020.03.10

在您的情况下,当您想在字符串的末尾(以插入逗号)时,也可以只使用该Number.toFixed()函数,但是,它将执行舍入。

StafanItachi 2020.03.10

截断

// Math.trunc() is part of the ES6 spec
Math.trunc( 1.5 );  // returns 1
Math.trunc( -1.5 ); // returns -1
// Math.floor( -1.5 ) would return -2, which is probably not what you wanted

为了

Math.round( 1.5 );  // 2
Math.round( 1.49 ); // 1
Math.round( -1.6 ); // -2
Math.round( -1.3 ); // -1
飞云卡卡西 2020.03.10

另一种可能的方式-使用XOR操作:

console.log(12.3 ^ 0); // 12
console.log("12.3" ^ 0); // 12
console.log(1.2 + 1.3 ^ 0); // 2
console.log(1.2 + 1.3 * 2 ^ 0); // 3
console.log(-1.2 ^ 0); // -1
console.log(-1.2 + 1 ^ 0); // 0
console.log(-1.2 - 1.3 ^ 0); // -2

按位运算的优先级小于数学运算的优先级,这很有用。尝试https://jsfiddle.net/au51uj3r/

LEYJim 2020.03.10

如果查看MathJavaScript中的本机对象,您会获得一整套处理数字和值等的函数。

基本上,您想要做的是非常简单的JavaScript本机...

假设您有以下电话号码:

const myValue = 56.4534931;

现在,如果您想将其四舍五入到最接近的数字,只需执行以下操作:

const rounded = Math.floor(myValue);

你会得到:

56

如果您想将其四舍五入到最接近的数字,请执行以下操作:

const roundedUp = Math.ceil(myValue);

你会得到:

57

Math.round可以将其四舍五入为更高或更低的数字,取决于哪个更接近浮点数。

您也可以~~在浮点数后面使用,将浮点数转换为整数。

您可以像~~myValue... 一样使用它

卡卡西乐逆天 2020.03.10

位移0等于除以1

// >> or >>>
2.0 >> 0; // 2
2.0 >>> 0; // 2
小小Near阳光 2020.03.10

您可以使用parseInt方法不进行舍入。由于0x(十六进制)和0(八进制)前缀选项,用户输入时要小心。

var intValue = parseInt(floatValue, 10);
Tom凯 2020.03.10

按位或运算符

可以使用按位或运算符截断浮点数,它既可以用于正数也可以用于负数:

function float2int (value) {
    return value | 0;
}

结果

float2int(3.1) == 3
float2int(-3.1) == -3
float2int(3.9) == 3
float2int(-3.9) == -3

性能比较?

我创建了一个JSPerf测试,用于比较以下两者之间的性能:

  • Math.floor(val)
  • val | 0 按位
  • ~~val 按位
  • parseInt(val)

仅适用于正数。在这种情况下,可以安全地使用按位运算以及Math.floor函数。

但是,如果您需要代码同时使用正数和负数,那么按位运算是最快的(或首选)。另一个JSPerf测试进行了比较,很明显,由于进行了额外的符号检查,Math现在是这四个中最慢的。

注意

As stated in comments, BITWISE operators operate on signed 32bit integers, therefore large numbers will be converted, example:

1234567890  | 0 => 1234567890
12345678901 | 0 => -539222987
伽罗理查德 2020.03.10

对于截断:

var intvalue = Math.floor(value);

对于回合:

var intvalue = Math.round(value);
飞云前端西门 2020.03.10
var intvalue = Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue ); 
var intvalue = Math.round( floatvalue );

// `Math.trunc` was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue );

数学对象参考


例子

// value=x        //  x=5          5<x<5.5      5.5<=x<6  

Math.floor(value) //  5            5            5
Math.ceil(value)  //  5            6            6
Math.round(value) //  5            5            6
Math.trunc(value) //  5            5            5
parseInt(value)   //  5            5            5
~~value           //  5            5            5
value | 0         //  5            5            5
value >> 0        //  5            5            5
value >>> 0       //  5            5            5
value - value % 1 //  5            5            5
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6

Math.floor(value) // -5           -6           -6
Math.ceil(value)  // -5           -5           -5
Math.round(value) // -5           -5           -6
Math.trunc(value) // -5           -5           -5
parseInt(value)   // -5           -5           -5
value | 0         // -5           -5           -5
~~value           // -5           -5           -5
value >> 0        // -5           -5           -5
value >>> 0       // 4294967291   4294967291   4294967291
value - value % 1 // -5           -5           -5
正数-更大的数字
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1

// value=x            x=900719925474099    x=900719925474099.4  x=900719925474099.5

Math.floor(value) //  900719925474099      900719925474099      900719925474099
Math.ceil(value)  //  900719925474099      900719925474100      900719925474100
Math.round(value) //  900719925474099      900719925474099      900719925474100
Math.trunc(value) //  900719925474099      900719925474099      900719925474099
parseInt(value)   //  900719925474099      900719925474099      900719925474099
value | 0         //  858993459            858993459            858993459
~~value           //  858993459            858993459            858993459
value >> 0        //  858993459            858993459            858993459
value >>> 0       //  858993459            858993459            858993459
value - value % 1 //  900719925474099      900719925474099      900719925474099
负数-较大的数字
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

// value = x      // x=-900719925474099   x=-900719925474099.5 x=-900719925474099.6

Math.floor(value) // -900719925474099     -900719925474100     -900719925474100
Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099
Math.round(value) // -900719925474099     -900719925474099     -900719925474100
Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099
parseInt(value)   // -900719925474099     -900719925474099     -900719925474099
value | 0         // -858993459           -858993459           -858993459
~~value           // -858993459           -858993459           -858993459
value >> 0        // -858993459           -858993459           -858993459
value >>> 0       //  3435973837           3435973837           3435973837
value - value % 1 // -900719925474099     -900719925474099     -900719925474099
小小神无 2020.03.10

注意:您不能将Math.floor()其用作截断的替代品,因为Math.floor(-3.1) = -4而不是-3!!

截断的正确替代是:

function truncate(value)
{
    if (value < 0) {
        return Math.ceil(value);
    }

    return Math.floor(value);
}
蛋蛋樱 2020.03.10

可以使用按位not运算符截断浮点数。你提到的其他操作都可以通过Math.floorMath.ceilMath.round

> ~~2.5
2
> ~~(-1.4)
-1

更多细节由James Padolsey提供。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android