I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. And efficiently, not via converting to a string and parsing.
I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. And efficiently, not via converting to a string and parsing.
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
Negative
// 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
Positive - Larger numbers
// 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
Negative - Larger numbers
// 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
For truncate:
var intvalue = Math.floor(value);
For round:
var intvalue = Math.round(value);
//Convert a float to integer
Math.floor(5.95)
//5
Math.ceil(5.95)
//6
Math.round(5.4)
//5
Math.round(5.5)
//6
Math.trunc(5.5)
//5
//Quick Ways
console.log(5.95| 0)
console.log(~~5.95)
console.log(5.95 >> 0)
//5
Math.floor() function returns the largest integer less than or equal to a given number.
console.log('Math.floor : ', Math.floor(3.5));
console.log('Math.floor : ', Math.floor(-3.5));
Math.ceil() function always rounds a number up to the next largest integer.
console.log('Math.ceil : ', Math.ceil(3.5));
console.log('Math.ceil : ', Math.ceil(-3.5));
Math.round() function returns the value of a number rounded to the nearest integer.
console.log('Math.round : ', Math.round(3.5));
console.log('Math.round : ', Math.round(-3.5));
Math.trunc() function returns the integer part of a number by removing any fractional digits.
console.log('Math.trunc : ', Math.trunc(3.5));
console.log('Math.trunc : ', Math.trunc(-3.5));
Today 2020.11.28 I perform tests on MacOs HighSierra 10.13.6 on Chrome v85, Safari v13.1.2 and Firefox v80 for chosen solutions.
I perform test case which you can run HERE
Below snippet presents differences between solutions A B C D E F G H I J K L
function A(float) {
return Math.trunc( float );
}
function B(float) {
return parseInt(float);
}
function C(float) {
return float | 0;
}
function D(float) {
return ~~float;
}
function E(float) {
return float >> 0;
}
function F(float) {
return float - float%1;
}
function G(float) {
return float ^ 0;
}
function H(float) {
return Math.floor( float );
}
function I(float) {
return Math.ceil( float );
}
function J(float) {
return Math.round( float );
}
function K(float) {
return float.toFixed(0);
}
function L(float) {
return float >>> 0;
}
// ---------
// TEST
// ---------
[A,B,C,D,E,F,G,H,I,J,K,L]
.forEach(f=> console.log(`${f.name} ${f(1.5)} ${f(-1.5)} ${f(2.499)} ${f(-2.499)}`))
This snippet only presents functions used in performance tests - it not perform tests itself!
And here are example results for chrome
If look into native Math object in JavaScript, you get the whole bunch of functions to work on numbers and values, etc...
Basically what you want to do is quite simple and native in JavaScript...
Imagine you have the number below:
const myValue = 56.4534931;
and now if you want to round it down to the nearest number, just simply do:
const rounded = Math.floor(myValue);
and you get:
56
If you want to round it up to the nearest number, just do:
const roundedUp = Math.ceil(myValue);
and you get:
57
Also Math.round just round it to higher or lower number depends on which one is closer to the flot number.
Also you can use of ~~ behind the float number, that will convert a float to a whole number.
You can use it like ~~myValue...
If you want a rounded off answer on the downward side:
var intvalue = Math.floor( floatvalue );
var integer = Math.floor(4.56);
Answer = 4
If you want to round off upwards:
var intvalue = Math.ceil( floatvalue );
Answeer would be = 5