How to get the first digit of int number in TypeScript

2 Answers

0 votes
function getFirstDigit(num: number) {
    return Number(String(num)[0]);
}
 
const n: number = 97236;
  
let first_digit: number = getFirstDigit(n)
  
console.log(first_digit); 
        
         
    
     
/*
run:
      
9
         
*/

 



answered Apr 16, 2024 by avibootz
0 votes
const n: number = 97236;
   
const firstDigitStr: string = String(n)[0];
   
console.log(firstDigitStr); 
         
          
     
      
/*
run:
       
"9"
          
*/

 



answered Apr 16, 2024 by avibootz
...