How to get the number of digits before the decimal point in TypeScript

1 Answer

0 votes
function totalDigitsBeforeDecimalPoint(num : number) : number {
    num = Math.abs(num);
     
    return num.toString().split('.')[0].length;
}
 
console.log(totalDigitsBeforeDecimalPoint(3.14)); 
console.log(totalDigitsBeforeDecimalPoint(45.0)); 
console.log(totalDigitsBeforeDecimalPoint(-7.763)); 
console.log(totalDigitsBeforeDecimalPoint(9863.2)); 
console.log(totalDigitsBeforeDecimalPoint(847425));   
   
   
   
/*
run:
   
1 
2 
1 
4 
6 
   
*/

 



answered Jun 14, 2022 by avibootz
...