Skip to content

Instantly share code, notes, and snippets.

@LogIN-
Created October 4, 2014 11:09
Show Gist options
  • Save LogIN-/22738587922389a14f71 to your computer and use it in GitHub Desktop.
Save LogIN-/22738587922389a14f71 to your computer and use it in GitHub Desktop.
PHP Convert numbers to words
<?php
function ThreeDigitsInWords($Number){
if ($Number == 0){
return "";
}
$InWords = "";
$Units_Place = array("zero","one","two","three","four","five","six","seven","eight","nine");
$Tens_Place = array(1=>"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety");
$Number = str_repeat("0",3-strlen($Number)).$Number;
if ($Number[0].$Number[1] == 0){
return $Units_Place[$Number[2]];
}
if ($Number[0] != 0){
$InWords .= $Units_Place[$Number[0]]." hundred";
if ($Number[1].$Number[2] != 0){
$InWords .= " and";
}
}
if ($Number[1].$Number[2] == 0){
return $InWords;
}
if ($Number[2] == 0){
return " ".$InWords." ".$Tens_Place[$Number[2]];
}
if ($Number[1].$Number[2] >= 11 && $Number[1].$Number[2] <= 19){
switch ($Number[1].$Number[2]){
case "11":
return $InWords." eleven";
break;
case "12":
return $InWords." twelve";
break;
case "13":
return $InWords." thirteen";
break;
case "14":
return $InWords." fourteen";
break;
case "15":
return $InWords." fifteen";
break;
case "16":
return $InWords." sixteen";
break;
case "17":
return $InWords." seventeen";
break;
case "18":
return $InWords." eighteen";
break;
case "19":
return $InWords." nineteen";
break;
}
}else{
$InWords .= " ".$Tens_Place[$Number[1]];
}
return $InWords." ".$Units_Place[$Number[2]];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment