Advertisement
fueanta

Integer to String in TS.

May 26th, 2020 (edited)
1,655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const numToString = (num: number): string => {
  2.     const sign = num < 0 ? '-' : '';
  3.     let myNum = num < 0 ? (-1 * num) : num;
  4.     let output = "";
  5.  
  6.     while (myNum > 0) {
  7.         const lastDigit = myNum % 10;
  8.         output = String.fromCharCode(lastDigit + 48) + output;
  9.         myNum = ~~(myNum / 10);
  10.     }
  11.  
  12.     return sign + output;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement