Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function num2english()
- {
- var retStr='';
- var str='';
- var place=0;
- var modBox=0;
- var hundredsInt=0;
- var tensInt=0;
- var onesInt=0;
- var placeName = [' thousand',' million',' billion',' trillion',' quadrillion'];
- var tens = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'];
- var teens = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eightteen','nineteen'];
- var ones = ['','one','two','three','four','five','six','seven','eight','nine'];
- var num = parseInt(prompt('Enter a number less than 1 qintillion to translate','')); //get user input
- do{
- modBox = (num % 1000);
- if(modBox)
- {
- if(place) retStr = ', ' + retStr;
- hundredsInt = Math.floor(modBox/100);
- onesInt = modBox % 10;
- tensInt = ((modBox - (hundredsInt * 100)) - onesInt) / 10;
- switch(tensInt)
- {
- case 0:
- if(onesInt) str = ones[onesInt];
- break;
- case 1:
- str = teens[onesInt];
- break;
- default:
- str = tens[tensInt - 2];
- if(onesInt)
- {
- str = str + '-' + ones[onesInt];
- }
- }
- if(place)
- {
- str = hundredsInt ? ones[hundredsInt] + ' hundred ' + str + placeName[place - 1] : str + placeName[place - 1];
- }else{
- if(hundredsInt)
- {
- str = ones[hundredsInt] + ' hundred ' + str;
- }
- }
- }
- retStr = str + retStr;
- str = '';
- place++;
- num = Math.floor(num/1000);
- }while(num);
- alert(retStr);
- }
- num2english();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement