I want to take 2 digits after float like below example:
function cut() {
var num1=8.844
// should be return 8.84
var num2=8.847
// should be return 8.84
}
I want to take 2 digits after float like below example:
function cut() {
var num1=8.844
// should be return 8.84
var num2=8.847
// should be return 8.84
}
You could multiply the value with 100, take the floorished value and divide by 100.
You may witness the wonderful world of floating point arithmetic, where numbers have some more or less decimals, like Is floating point math broken?
function cut(value) {
return Math.floor(value * 100) / 100;
}
console.log(cut(8.844));
console.log(cut(8.847));