How to get the number of digits before the decimal point in Node.js

1 Answer

0 votes
function totalDigitsBeforeDecimalPoint(num) {
    num = Math.abs(num);
     
    return num.toString().split('.')[0].length;
}
 
console.log(totalDigitsBeforeDecimalPoint(3.14)); 
console.log(totalDigitsBeforeDecimalPoint(97.0)); 
console.log(totalDigitsBeforeDecimalPoint(-1.846)); 
console.log(totalDigitsBeforeDecimalPoint(5363.2)); 
console.log(totalDigitsBeforeDecimalPoint(94580));   
   
   
   
   
/*
run:
   
1
2
1
4
5 
   
*/

 



answered Jun 14, 2022 by avibootz
...