Title / Description
Code <?php /* so let's say you are given an input : 3789 the program should output : "Three thousand seven hundred eighty nine" feel free to use pseudo code bugs also aren't an issue The easiest, and quickest way would be to use the PEAR package Numbers_Words http://pear.php.net/package-info.php?package=Numbers_Words but i am assuming for this situation you are looking for a manual mapping. edge cases: 10 - 19 */ print nameForNumber(3789, 'de')."\n"; /** * Translate integer number to string Word version * @param Integer $inputString * @param String $language: 'en' = english (default), 'de' = german * @return String */ function nameForNumber($inputString, $language = 'en') { $dictionary['en']['std'] = array(0 => '', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten', 11 => 'eleven', 12 => 'twelve', 13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen', 16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen', 19 => 'nineteen', 20 => 'twenty', 30 => 'thirty', 40 => 'fourty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'); $dictionary['en']['thousand'] = 'thousand'; $dictionary['en']['hundred'] = 'hundred'; $dictionary['de']['std'] = array(0 => '', 1 => 'ein', 2 => 'zwei', 3 => 'drei', 4 => 'vier', 5 => 'fuenf', 6 => 'sechs', 7 => 'sieben', 8 => 'acht', 9 => 'neun', 10 => 'zehn', 11 => 'elf', 12 => 'zwoelf', 13 => 'dreizehn', 14 => 'vierzehn', 15 => 'fuenfzehn', 16 => 'sechzehn', 17 => 'siebzehn', 18 => 'achzehn', 19 => 'neunzehn', 20 => 'zwanzig', 30 => 'dreissig', 40 => 'vierzig', 50 => 'fuenfzig', 60 => 'sechzig', 70 => 'siebzig', 80 => 'achzig', 90 => 'neunzig'); $dictionary['de']['thousand'] = 'tausend'; $dictionary['de']['hundred'] = 'hundert'; $thousand = (floor($inputString/1000)); $reminder = ($inputString % 1000); $hundred = (floor($reminder/100)); $reminder = ($reminder % 100); $ten = (floor($reminder/10)); $one = ($reminder % 10); var_dump($one); $returnString = ""; if($thousand >0){ $returnString .= $dictionary[$language]['std'][$thousand].$dictionary[$language]['thousand']; } if($hundred >0){ $returnString .= $dictionary[$language]['std'][$hundred].$dictionary[$language]['hundred']; } if($ten >1){ $realTen = (10*$ten); if($language == 'en') { $returnString .= $dictionary[$language]['std'][$realTen]; if($one >0) { $returnString .= $dictionary[$language]['std'][$one]; } } if($language == 'de') { if($one >0) { $returnString .= $dictionary[$language]['std'][$one]."und"; } $returnString .= $dictionary[$language]['std'][$realTen]; } }else{ $realTen = ((10*$ten) + $one); $returnString .= $dictionary[$language]['std'][$realTen]; if($realTen == 1 && $language == 'de') { $returnString .= 's'; } } return $returnString; }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code