xxxxxxxxxx
// Using parseInt() to convert to an integer (rounding down)
var floatNumber = 3.14;
var intNumber = parseInt(floatNumber);
console.log(intNumber); // Output: 3
// Using Math.floor() to convert to an integer (truncating)
floatNumber = 3.99;
intNumber = Math.floor(floatNumber);
console.log(intNumber); // Output: 3
xxxxxxxxxx
function float2int (value) {
return value | 0;
}
float2int(3.75); //3 - always just truncates decimals
//other options
Math.floor( 3.75 );//3 - goes to floor , note (-3.75 = -4)
Math.ceil( 3.75 ); //4 - goes to ceiling, note (-3.75 = -3)
Math.round( 3.75 );//4
xxxxxxxxxx
// 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
xxxxxxxxxx
var floatNumber = 3.14;
var integerNumber = Math.floor(floatNumber); // "Math.floor" rounds down the number
console.log(integerNumber); // Output: 3
xxxxxxxxxx
// Declare a float variable
var myFloat = 3.14;
// Perform some operations with float
var result = myFloat * 2;
result += 1.5;
// Print the result
console.log(result);