vonko1988

JSNameOfNumber

May 2nd, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     input = parseInt(input);
  2.             input = String(input);
  3.             var n2 = parseInt(input % 100) % 10;
  4.             var n1 = parseInt(Math.floor(input / 10)) % 10;
  5.             var n0 = parseInt(Math.floor(input / 100)) % 10;
  6.  
  7.             var ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
  8.                         'Seven', 'Eight', 'Nine'];
  9.             var tens = ['', 'Ten', 'Twenty', 'Thirty', 'Fourty', 'Fifty',
  10.                         'Sixty', 'Seventy', 'Eighty', 'Ninety'];
  11.             var specialNumbers = ['', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen',
  12.                                  'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
  13.  
  14.             var length = input.length;
  15.  
  16.             if (length === 1) {
  17.                 if (n2 === 0) {
  18.                     output = 'Zero';
  19.                 }
  20.                 else {
  21.                     output = ones[n2];
  22.                 }
  23.             }
  24.  
  25.             else if (length === 2) {
  26.                 if (n1 === 1) {
  27.                     output = specialNumbers[n2];
  28.                 }
  29.                 if (n1 !== 1) {
  30.  
  31.                     output = tens[n1] + ' ' + ones[n2];
  32.                 }              
  33.             }
  34.  
  35.             else if (length == 3) {
  36.                 if (n1 === 1) {
  37.                     output = ones[n0] + ' Hundred and ' + specialNumbers[n2];
  38.                 }
  39.                 else {
  40.                     if (n1 !== 0 || n2 !== 0) {
  41.                         output = ones[n0] + ' Hundred and ' + tens[n1] + ' ' + ones[n2];
  42.                     }
  43.                     else {
  44.                         output = ones[n0] + ' Hundred';
  45.                     }
  46.                 }
  47.             }
Advertisement
Add Comment
Please, Sign In to add comment