/** * English Number Converter - Collection of PHP functions to convert a number * into English text. * * This exact code is licensed under CC-Wiki on Stackoverflow. * http://creativecommons.org/licenses/by-sa/3.0/ * * @link http://stackoverflow.com/q/277569/367456 * @question Is there an easy way to convert a number to a word in PHP? * * This file incorporates work covered by the following copyright and * permission notice: * * Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text * You can use this freely and modify it however you want. */ function convertNumber($number) { //list($integer,$fraction) = explode(".", (string) $number); list($integer) = explode(".", (string) $number); $output = ""; if ($integer[0] == "-") { $output = "negative "; $integer = ltrim($integer, "-"); } else { if ($integer[0] == "+") { $output = "positive "; $integer = ltrim($integer, "+"); } } if ($integer[0] == "0") { $output .= "zero"; } else { $integer = str_pad($integer, 36, "0", STR_PAD_LEFT); $group = rtrim(chunk_split($integer, 3, " "), " "); $groups = explode(" ", $group); $groups2 = array(); foreach ($groups as $g) { $groups2[] = convertThreeDigit($g[0], $g[1], $g[2]); } for ($z = 0; $z < count($groups2); $z++) { if ($groups2[$z] != "") { $output .= $groups2[$z] . convertGroup(11 - $z) . ($z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11][0] == '0' ? " and " : ", "); } } $output = rtrim($output, ", "); } // if ($fraction > 0) // { // $output .= " point"; // for ($i = 0; $i < strlen($fraction); $i++) // { // $output .= " " . convertDigit($fraction{$i}); // } // } return $output; }
function convertNumber($num) { list($num, $dec) = explode(".", $num); $output = ""; if ($num[0] == "-") { $output = "negative "; $num = ltrim($num, "-"); } else { if ($num[0] == "+") { $output = "positive "; $num = ltrim($num, "+"); } } if ($num[0] == "0") { $output .= "zero"; } else { $num = str_pad($num, 36, "0", STR_PAD_LEFT); $group = rtrim(chunk_split($num, 3, " "), " "); $groups = explode(" ", $group); $groups2 = array(); foreach ($groups as $g) { $groups2[] = convertThreeDigit($g[0], $g[1], $g[2]); } for ($z = 0; $z < count($groups2); $z++) { if ($groups2[$z] != "") { $output .= $groups2[$z] . convertGroup(11 - $z) . ($z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11][0] == '0' ? " " : " "); // && $groups2[11] != '' && $groups[11]{0} == '0' ? " and " : ", "); } } $output = rtrim($output, ", "); } if ($dec > 0) { $output .= " and cents " . convertTwoDigit($dec[0], $dec[1]); } return strtoupper($output . " only"); }
function convertNumber($num) { $dec = strpos($num, '.'); if ($dec !== false) { list($num, $dec) = explode('.', $num); } else { $dec = 0; } $output = ""; if ($num[0] == "-") { $output .= "negative "; $num = ltrim($num, '-'); } else { if ($num[0] == "+") { $output .= "positive "; $num = ltrim($num, '+'); } } if ($num[0] == "0") { $output .= " zero"; } else { $num = str_pad($num, 36, "0", STR_PAD_LEFT); $group = rtrim(chunk_split($num, 3, " "), " "); $groups = explode(" ", $group); $groups2 = array(); foreach ($groups as $g) { $groups2[] = convertThreeDigit($g[0], $g[1], $g[2]); } for ($z = 0; $z < count($groups2); $z++) { if ($groups2[$z] != "") { $output .= $groups2[$z] . convertGroup(11 - $z) . ($z < 11 && !array_search("", array_slice($groups2, $z + 1, -1)) && $groups2[11] != "" && $groups[11][0] == '0' ? " and " : ", "); } } $output = rtrim($output, ", "); } if ($dec > 0) { $output .= " point"; for ($i = 0; $i < strlen($dec); $i++) { $output .= " " . convertDigit($dec[$i]); } } return $output; }