SHOW:
|
|
- or go back to the newest paste.
| 1 | function num2english() | |
| 2 | {
| |
| 3 | var retStr=''; | |
| 4 | var str=''; | |
| 5 | var place=0; | |
| 6 | var modBox=0; | |
| 7 | var hundredsInt=0; | |
| 8 | var tensInt=0; | |
| 9 | var onesInt=0; | |
| 10 | - | var placeName = [' thousand',' million',' billion',' trillion',' quadrillion']; |
| 10 | + | var placeName = [' thousand',' million',' billion',' trillion',' quadrillion']; |
| 11 | var tens = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']; | |
| 12 | var teens = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eightteen','nineteen']; | |
| 13 | var ones = ['','one','two','three','four','five','six','seven','eight','nine']; | |
| 14 | ||
| 15 | var num = parseInt(prompt('Enter a number less than 1 qintillion to translate','')); //get user input
| |
| 16 | ||
| 17 | do{
| |
| 18 | modBox = (num % 1000); | |
| 19 | ||
| 20 | if(modBox) | |
| 21 | {
| |
| 22 | if(place) retStr = ', ' + retStr; | |
| 23 | ||
| 24 | hundredsInt = Math.floor(modBox/100); | |
| 25 | onesInt = modBox % 10; | |
| 26 | tensInt = ((modBox - (hundredsInt * 100)) - onesInt) / 10; | |
| 27 | ||
| 28 | switch(tensInt) | |
| 29 | {
| |
| 30 | case 0: | |
| 31 | if(onesInt) str = ones[onesInt]; | |
| 32 | break; | |
| 33 | case 1: | |
| 34 | str = teens[onesInt]; | |
| 35 | break; | |
| 36 | default: | |
| 37 | str = tens[tensInt - 2]; | |
| 38 | if(onesInt) | |
| 39 | {
| |
| 40 | str = str + '-' + ones[onesInt]; | |
| 41 | } | |
| 42 | } | |
| 43 | - | if(place) |
| 43 | + | if(place) |
| 44 | {
| |
| 45 | str = hundredsInt ? ones[hundredsInt] + ' hundred ' + str + placeName[place - 1] : str + placeName[place - 1]; | |
| 46 | }else{
| |
| 47 | if(hundredsInt) | |
| 48 | {
| |
| 49 | str = ones[hundredsInt] + ' hundred ' + str; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | } | |
| 54 | ||
| 55 | retStr = str + retStr; | |
| 56 | ||
| 57 | str = ''; | |
| 58 | ||
| 59 | place++; | |
| 60 | ||
| 61 | num = Math.floor(num/1000); | |
| 62 | ||
| 63 | }while(num); | |
| 64 | ||
| 65 | alert(retStr); | |
| 66 | } | |
| 67 | ||
| 68 | num2english(); |