Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function convert_number_to_words($number) {
- $hyphen = '-';
- $conjunction = ' ';
- $separator = ' ';
- $decimal = ' point ';
- $dictionary = array(
- 0 => 'Zero',
- 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',
- 100 => 'Hundred',
- 1000 => 'Thousand',
- 1000000 => 'Million',
- );
- if (!is_numeric($number)) {
- return false;
- }
- if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
- echo "Max. of 7 digits only!";
- return false;
- }
- if ($number >= 10000000) {
- echo "Max. of 7 digits only!"; goto end;
- }
- if ($number < 0) {
- echo "Negative Values not Accepted"; goto end;
- }
- $string = $fraction = null;
- if (strpos($number, '.') !== false) {
- list($number, $fraction) = explode('.', $number);
- }
- switch (true) {
- case $number < 21:
- $string = $dictionary[$number];
- break;
- case $number < 100:
- $tens = ((int) ($number / 10)) * 10;
- $units = $number % 10;
- $string = $dictionary[$tens];
- if ($units) {
- $string .= $hyphen . $dictionary[$units];
- }
- break;
- case $number < 1000:
- $hundreds = $number / 100;
- $remainder = $number % 100;
- $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
- if ($remainder) {
- $string .= $conjunction . convert_number_to_words($remainder);
- }
- break;
- default:
- $baseUnit = pow(1000, floor(log($number, 1000)));
- $numBaseUnits = (int) ($number / $baseUnit);
- $remainder = $number % $baseUnit;
- $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
- if ($remainder) {
- $string .= $remainder < 100 ? $conjunction : $separator;
- $string .= convert_number_to_words($remainder);
- }
- break;
- }
- if (null !== $fraction && is_numeric($fraction)) {
- $string .= $decimal;
- $words = array();
- foreach (str_split((string) $fraction) as $number) {
- $words[] = $dictionary[$number];
- }
- $string .= implode(' ', $words);
- }
- return $string;
- end:
- }
- echo '<b>'.convert_number_to_words($_POST['num']).'</b>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment