fueanta

Integer to String in TS.

May 26th, 2020 (edited)
1,543
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. }
RAW Paste Data