var intvalue = Math.floor( floatvalue );var intvalue = Math.ceil( floatvalue );var intvalue = Math.round( floatvalue );// `Math.trunc` was added in ECMAscript 6var intvalue = Math.trunc( floatvalue );
例子
Positive
// value=x // x=5 5<x<5.5 5.5<=x<6Math.floor(value) // 5 5 5Math.ceil(value) // 5 6 6Math.round(value) // 5 5 6Math.trunc(value) // 5 5 5parseInt(value) // 5 5 5~~value// 5 5 5value | 0 // 5 5 5value >> 0 // 5 5 5value >>> 0 // 5 5 5value - value % 1 // 5 5 5
Negative
// value=x // x=-5 -5>x>=-5.5 -5.5>x>-6Math.floor(value) // -5-6-6Math.ceil(value) // -5-5-5Math.round(value) // -5-5-6Math.trunc(value) // -5-5-5parseInt(value) // -5-5-5value | 0 // -5-5-5~~value// -5-5-5value >> 0 // -5-5-5value >>> 0 // 4294967291 4294967291 4294967291value - value % 1 // -5-5-5
Positive - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1// value=x x=900719925474099 x=900719925474099.4 x=900719925474099.5Math.floor(value) // 900719925474099 900719925474099 900719925474099Math.ceil(value) // 900719925474099 900719925474100 900719925474100Math.round(value) // 900719925474099 900719925474099 900719925474100Math.trunc(value) // 900719925474099 900719925474099 900719925474099parseInt(value) // 900719925474099 900719925474099 900719925474099value | 0 // 858993459 858993459 858993459~~value// 858993459 858993459 858993459value >> 0 // 858993459 858993459 858993459value >>> 0 // 858993459 858993459 858993459value - value % 1 // 900719925474099 900719925474099 900719925474099
Negative - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1// value = x // x=-900719925474099 x=-900719925474099.5 x=-900719925474099.6Math.floor(value) // -900719925474099 -900719925474100 -900719925474100Math.ceil(value) // -900719925474099 -900719925474099 -900719925474099Math.round(value) // -900719925474099 -900719925474099 -900719925474100Math.trunc(value) // -900719925474099 -900719925474099 -900719925474099parseInt(value) // -900719925474099 -900719925474099 -900719925474099value | 0 // -858993459-858993459-858993459~~value// -858993459-858993459-858993459value >> 0 // -858993459-858993459-858993459value >>> 0 // 343597383734359738373435973837value - value % 1 // -900719925474099 -900719925474099 -900719925474099



