我想在JavaScript中将浮点数转换为整数。实际上,我想知道如何进行两种标准转换:通过截断和舍入。而且高效,而不是通过转换为字符串和解析。
当前回答
在您的例子中,当您希望在末尾插入字符串(以便插入逗号)时,也可以只使用Number.toFixed()函数,但这将执行舍入。
其他回答
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
双位not运算符可用于截断浮点数。您提到的其他操作可通过Math.floor、Math.ceil和Math.round获得。
> ~~2.5
2
> ~~(-1.4)
-1
更多细节由詹姆斯·帕多尔西提供。
函数返回小于或等于给定数字的最大整数。console.log('数学楼层:',数学楼层(3.5));console.log('数学楼层:',数学楼层(-3.5));Math.ceil()函数始终将数字舍入到下一个最大整数。console.log('Math.ceil:',Math.ceil(3.5));console.log('Math.ceil:',Math.ceil(-3.5));函数的作用是返回舍入到最接近整数的数值。console.log('Math.round:',Math.round(3.5));console.log('Math.round:',Math.round(-3.5));函数的作用是通过删除任何小数来返回数字的整数部分。console.log('Math.trunc:',Math.ttrunc(3.5));console.log('Math.trunc:',Math.ttrunc(-3.5));
如果您想要向下四舍五入的答案:
var intvalue = Math.floor( floatvalue );
var integer = Math.floor(4.56);
Answer = 4
如果要向上舍入:
var intvalue = Math.ceil( floatvalue );
Answeer would be = 5
要截断:
//Math.ctrunk()是ES6规范的一部分console.log(数学截断(1.5));//返回1console.log(数学截断(-1.5));//返回-1//数学floor(-1.5)将返回-2,这可能不是您想要的
要舍入:
console.log(数学舍入(1.5));//2.console.log(数学舍入(1.49));//1.console.log(数学舍入(-1.6));//-2.console.log(数学舍入(-1.3));//-1.