Centinul Posted June 28, 2007 Share Posted June 28, 2007 Is there an easy way in PHP to convert numbers to their textual descriptions without writing mountains of if/switch statements? For example I would need to do the following: 4 -> Four 11 -> Eleven 18 -> Eighteen Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/ Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 Not that I know of, it can be done with dates, but that doesn't work for numbers of 31. How high are your numbers going? Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285060 Share on other sites More sharing options...
no_one Posted June 28, 2007 Share Posted June 28, 2007 Couldn't find anything, so I wrote a quick example of how you could do it. I didn't to any sort of optimization/etc. I used arrays to remove the need for a separate function w/ switches and/or if's. Just an idea. <?php (c) no_one $int1Str = array("zero","one","two","three","four","five","six","seven","eight","nine"); $int10Str = array(1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty",7=>"seventy",8=>"eighty",9=>"ninety"); function intString($num) { global $int1Str, $int10Str; $hold = $num; $istr = ""; while( $hold !== true ) { if( $hold >= 100 ) { if ( $hold >= 1000 ) { $hmult = 1000; $hms = " thousand "; } else { // hundred $hmult = 100; $hms = " hundred "; } $hm = (int)($hold/$hmult); $hold -= $hm*$hmult; $istr .= $int1Str[$hm] . $hms; } else if ( $hold >= 10 ) { $hm = (int)($hold/10); $hold -= $hm*10; // could probably do something better here $istr .= $int10Str[$hm] . " "; } else { if ( $hold || (!$hold && empty($istr)) ) { // sloppy? $istr .= $int1Str[$hold]; } $hold = true; // kill loop } } return $istr; } print("<br/>" . intString(8137)); ?> eight thousand one hundred thirty seven Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285144 Share on other sites More sharing options...
Wildbug Posted June 28, 2007 Share Posted June 28, 2007 Don't forget the *teens. Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285154 Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 no_one Mind if I use that script on my site? give you credit for it? Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285156 Share on other sites More sharing options...
no_one Posted June 28, 2007 Share Posted June 28, 2007 Ah yeah, the teens.. I'll go fix that right now. Go for it cooldude, let me fix it first :S Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285163 Share on other sites More sharing options...
no_one Posted June 28, 2007 Share Posted June 28, 2007 not much needed a change, ten through nineteen should come through now. Ty for the heads up, kind of hacked it out quick. <?php $int1Str = array("zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"); $int10Str = array(1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty",7=>"seventy",8=>"eighty",9=>"ninety"); function intString($num) { global $int1Str, $int10Str; $hold = $num; $istr = ""; while( $hold !== true ) { if( $hold >= 100 ) { if ( $hold >= 1000 ) { $hmult = 1000; $hms = " thousand "; } else { // hundred $hmult = 100; $hms = " hundred "; } $hm = (int)($hold/$hmult); $hold -= $hm*$hmult; $istr .= $int1Str[$hm] . $hms; } else if ( $hold >= 20 ) { $hm = (int)($hold/10); $hold -= $hm*10; // could probably do something better here $istr .= $int10Str[$hm] . " "; } else { if ( $hold || (!$hold && empty($istr)) ) { $istr .= $int1Str[$hold]; } $hold = true; // kill loop } } return $istr; } ?> Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285169 Share on other sites More sharing options...
cooldude832 Posted June 28, 2007 Share Posted June 28, 2007 Thanks, I'm working on a php document with a whole bunch of good functions like this that I can share with people. Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285199 Share on other sites More sharing options...
Centinul Posted June 29, 2007 Author Share Posted June 29, 2007 I would like to thank you all for the help. I will take the ideas you all posted and modify them to suit my needs. Thanks again. Link to comment https://forums.phpfreaks.com/topic/57594-converting-numbers-to-textual-descriptions/#findComment-285706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.