我想在JavaScript中将浮点数转换为整数。实际上,我想知道如何进行两种标准转换:通过截断和舍入。而且高效,而不是通过转换为字符串和解析。
当前回答
在您的例子中,当您希望在末尾插入字符串(以便插入逗号)时,也可以只使用Number.toFixed()函数,但这将执行舍入。
其他回答
如果查看JavaScript中的本机Math对象,您将得到一系列处理数字和值等的函数。。。
基本上,你想做的是非常简单的,并且是JavaScript中的原生代码。。。
假设您有以下数字:
const myValue = 56.4534931;
现在,如果您想将其舍入到最接近的数字,只需执行以下操作:
const rounded = Math.floor(myValue);
你会得到:
56
如果要将其舍入到最接近的数字,只需执行以下操作:
const roundedUp = Math.ceil(myValue);
你会得到:
57
此外,Math.round将其舍入为更高或更低的数字取决于哪个更接近flot数。
此外,还可以在浮点数后面使用~~,这会将浮点数转换为整数。
你可以像~~myValue一样使用它。。。
我只想指出,在金钱上,你想要圆,而不是截断。因为4.999452*100四舍五入会给你5,一个更具代表性的答案,所以少一分钱的可能性要小得多。
最重要的是,不要忘了银行家的舍入,这是一种抵消直接舍入带来的轻微正偏差的方法——您的金融应用可能需要它。
JavaScript中的高斯/银行家舍入
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
对于截断:
var intvalue = Math.floor(value);
对于圆形:
var intvalue = Math.round(value);
还有一种可能的方法-使用XOR运算:
console.log(12.3^0);//12console.log(“12.3”^0);//12console.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);//0console.log(-1.2-1.3^0);//-2.
按位运算的优先级低于数学运算的优先级,这很有用。试穿https://jsfiddle.net/au51uj3r/