29Mar2011
Number to Word Converter
So I recently found a function that has one parameter, which then goes into a switch/case statement for each individual number up till 31 giving the real word for the day. For example the input “31″ would return “thirty one”. I figured there was a much easier way to make this function. I ended up making two functions. I converted this to JavaScript from PHP so I can use it in a few different places. If you want to go past trillion all you need to do is add more words to the “triples” variables.
PHP
class numberConverter { protected $singles = array(); protected $doubles = array(); protected $triples = array(); public function __construct() { $this->singles = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventen', 'eighteen', 'nineteen'); $this->doubles = array('twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'); $this->triples = array('hundred','thousand', 'million', 'billion', 'trillion'); } public function convertNumber($number) { /* Splits the number into 3 digit sections Appends the correct prefix to that number Example: xxx,xxx,xxx (xxx) million (xxx) thousand (xxx) */ $length = strlen($number); // number.length; $len = $length%3; // 7 digits split by 3 = 7%3 = 1 remaineder - the first digit is off $len = ($len==0)?3:$len; // 0 = 3; $pos = 0; $rounds = ceil($length/3); // Math.ceil $ret = ""; for($i = 1;$i<=$rounds;$i++) { $nameIndex = $rounds-$i; // 3 rounds, first round, 3rd index "million" $thisThree = substr($number, $pos, $len); $ret .= $this->convertThree($thisThree); if($nameIndex >= 1 && $thisThree != 0) { $ret .= $this->triples[$nameIndex] . ' '; } $pos += $len; $len = 3; } return $ret; } private function convertThree($number) { /* Converts 3 digits at a time Example: 123 One hundred twenty three */ $length = strlen($number); // Length of the number, typically three $pos = $length; // position $ret = ""; // return variable $lastTwo = substr($number, -2);// last two digits: 123 = 23; 23 = 23; for($i = 0;$i<$length;$i++) { $thisNumber = $number[$i]; // 1 if($thisNumber != 0) { if($pos == 3) { $ret .= $this->singles[$thisNumber-1] . ' ' . $this->triples[$pos-3] . ' '; // two hundred } else if($pos == 2 && $lastTwo <= 19 && $lastTwo >= 10) { $ret .= $this->singles[$lastTwo-1] . ' '; // sixteen break; // break the "for" loop } else if($pos == 2) { $ret .= $this->doubles[$thisNumber-2] . ' '; // twenty, sixty } else { $ret .= $this->singles[$thisNumber-1] . ' '; // three, four } } $pos--; } return $ret; } } $convert = new numberConverter(); echo $convert->convertNumber(1651613186); //1,651,613,186
JavaScript:
<script type="text/javascript" rel="javascript"> var singles = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventen', 'eighteen', 'nineteen']; var doubles = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; var triples = ['hundred','thousand', 'million', 'billion', 'trillion']; function convertNumber(number) { /* Splits the number into 3 digit sections Appends the correct prefix to that number Example: xxx,xxx (xxx) thousand (xxx) */ var nameIndex,thisThree; number = String(number); var length = number.length; var len = length%3; // 7 digits split by 3 = 7%3 = 1 remaineder - the first digit is off len = (len==0)?3:len; // 0 = 3; var pos = 0; var rounds = Math.ceil(length/3); var ret = ""; for(var i = 1;i<=rounds;i++) { nameIndex = rounds-i; // 3 rounds, first round, 3rd index "million" thisThree = number.substring(pos, len+pos); ret += convertThree(thisThree); if(nameIndex >= 1 && parseInt(thisThree) != 0) { ret += triples[nameIndex]+' '; } pos += len; len = 3; } return ret; } function convertThree(number) { /* Converts 3 digits at a time Example: 123 One hundred twenty three */ number = number; // The number var thisNumber = 0; var length = number.length; // Length of the number, typically three var pos = length; // position var ret = ""; // return variable var lastTwo = parseInt(number.substring(number.length-2)); // last two digits: 123 = 23; 23 = 23; for(var i = 0;i<length;i++) { thisNumber = number[i]; // 1 if(thisNumber != 0) { if(pos == 3) { ret += singles[thisNumber-1] + ' ' + triples[pos-3]+' '; // two hundred } else if(pos == 2 && lastTwo <= 19 && lastTwo >= 10) { ret += singles[lastTwo-1] + ' '; // sixteen break; // break the "for" loop } else if(pos == 2) { ret += doubles[thisNumber-2] + ' '; // twenty, sixty } else { ret += singles[thisNumber-1] + ' '; // three, four } } pos--; } return ret; } alert(convertNumber(1651613186)); //1,651,613,186 </script>

Discussion
No responses to "Number to Word Converter"
There are no comments yet, add one below.
Leave a Comment