I'm trying to check if a number inputed has over 8 decimal places and if it does then I want to round it back down to 8 decimal places. However, when I input the number 1.234001, it automatically rounds it to 8 decimal places. (1.234001 / 0.00000001) % 1 = 0 so I'm not sure why its rounding it. Here's my code
var SAT = 0.00000001;
if(!isNaN(input.value) && ((input.value / SAT) % 1 != 0)) {
input.value = parseFloat(input.value).toFixed(8);
console.log(6);
}
Try it in this way:
function nrOfDecimals(number) {
var match = (''+number).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
var decimals = Math.max(0,
(match[1] ? match[1].length : 0)
// Correct the notation.
- (match[2] ? +match[2] : 0));
if(decimals > 8){
//if decimal are more then 8
number = parseFloat(number).toFixed(8);
}
//else no adjustment is needed
return number;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.