js number to integer code example
Example 1: javascript convert float to int
function float2int (value) {
return value | 0;
}
float2int(3.75);
Math.floor( 3.75 );
Math.ceil( 3.75 );
Math.round( 3.75 );
Example 2: Javascript string to int
var myInt = parseInt("10.256");
var myFloat = parseFloat("10.256");
Example 3: converst strig in number in js
const numberInString = "20";
console.log(typeof(numberInString))
const numInNum = parseInt(numberInString)
console.log(typeof(numInNum))
Example 4: get the whole value of a number javascript
var value = 2.9802453587962963;
var wholeNum = Math.floor(value);
console.log(wholeNum);
Example 5: javascript number length
export function numberLength(number) {
let length = 0;
let n = Math.abs(number);
do {
n /= 10;
length++;
} while (n >= 1);
return length;
}
export default numberLength;
Example 6: js int
var myInt = 69420
1+1
1+"1"