number to text
Moderator: General Moderators
number to text
Does somebody now where to find function to convert number to text
example: 5,35 = five dolars and thirtyfive cents
example: 5,35 = five dolars and thirtyfive cents
Not tested with all possible types of numbers.
Code: Select all
<?php
echo AmtInWords("5,35");
function AmtInWords($Amt)
{
$Pieces = explode(",",$Amt);
$Dollars = $Pieces[0];
if (strlen($Dollars)%3 != 0)
$Dollars = str_repeat("0",3-(strlen($Dollars)%3)).$Dollars;
$Cents = $Pieces[1];
if (strlen($Cents)%3 != 0)
$Cents = str_repeat("0",3-(strlen($Cents)%3)).$Cents;
return NumberInWords($Dollars)." dollars and ".NumberInWords($Cents)." cents";
}
function NumberInWords($Number)
{
$InWords = "";
$Separator = array("","thousand","million","billion","trillion");
for ($i=strlen($Number)-1; $i>=0; $i-=3)
$InWords = ThreeDigitsInWords(substr($Number,$i-3+1,3))." ".$Separator[(strlen($Number)-$i-1)/3]." ".$InWords;
return $InWords;
}
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","eigthy","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]];
}
?>
Last edited by anjanesh on Mon May 23, 2005 10:35 am, edited 1 time in total.
here's another one:
Code: Select all
<?php
function NumberAsString($number)
{
$Strs = array();
$StrsA = array();
$Result=""; // Generated result
$Str1=""; // Temp string
$Str2=""; // Temp string
$n=$number; // Working copy
$Billions=0;
$Millions=0;
$Thousands=0;
$Hundreds=0;
$Tens=0;
$Ones=0;
$Point=0;
$HaveValue=0; // Flag needed to know if to process "0"
if ($number == 0 or strlen($number) == 0)
{
return "Zero";
}
// Initialize strings
// Strings are "externalized" to simplify
// changing text or translating
if (count($Strs)==0)
{
$Strs["space"]=" ";
$Strs["and"]="and";
$Strs["point"]="Point";
$Strs["n0"]="Zero";
$Strs["n1"]="One";
$Strs["n2"]="Two";
$Strs["n3"]="Three";
$Strs["n4"]="Four";
$Strs["n5"]="Five";
$Strs["n6"]="Six";
$Strs["n7"]="Seven";
$Strs["n8"]="Eight";
$Strs["n9"]="Nine";
$Strs["n10"]="Ten";
$Strs["n11"]="Eleven";
$Strs["n12"]="Twelve";
$Strs["n13"]="Thirteen";
$Strs["n14"]="Fourteen";
$Strs["n15"]="Fifteen";
$Strs["n16"]="Sixteen";
$Strs["n17"]="Seventeen";
$Strs["n18"]="Eighteen";
$Strs["n19"]="Nineteen";
$Strs["n20"]="Twenty";
$Strs["n30"]="Thirty";
$Strs["n40"]="Forty";
$Strs["n50"]="Fifty";
$Strs["n60"]="Sixty";
$Strs["n70"]="Seventy";
$Strs["n80"]="Eighty";
$Strs["n90"]="Ninety";
$Strs["n100"]="Hundred";
$Strs["nK"]="Thousand";
$Strs["nM"]="Million";
$Strs["nB"]="Billion";
}
// Save strings to an array once to improve performance
if (count($StrsA)==0)
{
// Arrays start at 1, to 1 contains 0
// 2 contains 1, and so on
$StrsA[1]=$Strs["n0"];
$StrsA[2]=$Strs["n1"];
$StrsA[3]=$Strs["n2"];
$StrsA[4]=$Strs["n3"];
$StrsA[5]=$Strs["n4"];
$StrsA[6]=$Strs["n5"];
$StrsA[7]=$Strs["n6"];
$StrsA[8]=$Strs["n7"];
$StrsA[9]=$Strs["n8"];
$StrsA[10]=$Strs["n9"];
$StrsA[11]=$Strs["n10"];
$StrsA[12]=$Strs["n11"];
$StrsA[13]=$Strs["n12"];
$StrsA[14]=$Strs["n13"];
$StrsA[15]=$Strs["n14"];
$StrsA[16]=$Strs["n15"];
$StrsA[17]=$Strs["n16"];
$StrsA[18]=$Strs["n17"];
$StrsA[19]=$Strs["n18"];
$StrsA[20]=$Strs["n19"];
$StrsA[21]=$Strs["n20"];
$StrsA[31]=$Strs["n30"];
$StrsA[41]=$Strs["n40"];
$StrsA[51]=$Strs["n50"];
$StrsA[61]=$Strs["n60"];
$StrsA[71]=$Strs["n70"];
$StrsA[81]=$Strs["n80"];
$StrsA[91]=$Strs["n90"];
}
// How many billions?
$Billions=floor($n/1000000000);
if ($Billions)
{
$n=$n-(1000000000*$Billions);
$Str1=NumberAsString($Billions).$Strs["space"].$Strs["nB"];
if (strlen($Result))
$Result=$Result.$Strs["space"];
$Result=$Result.$Str1;
$Str1="";
$HaveValue=1;
}
// How many millions?
$Millions=floor($n/1000000);
if ($Millions)
{
$n=$n-(1000000*$Millions);
$Str1=NumberAsString($Millions).$Strs["space"].$Strs["nM"];
if (strlen($Result))
$Result=$Result.$Strs["space"];
$Result=$Result.$Str1;
$Str1="";
$HaveValue=1;
}
// How many thousands?
$Thousands=floor($n/1000);
if ($Thousands)
{
$n=$n-(1000*$Thousands);
$Str1=NumberAsString($Thousands).$Strs["space"].$Strs["nK"];
if (strlen($Result))
$Result=$Result.$Strs["space"];
$Result=$Result.$Str1;
$Str1="";
$HaveValue=1;
}
// How many hundreds?
$Hundreds=floor($n/100);
if ($Hundreds)
{
$n=$n-(100*$Hundreds);
$Str1=NumberAsString($Hundreds).$Strs["space"].$Strs["n100"];
if (strlen($Result))
$Result=$Result.$Strs["space"];
$Result=$Result.$Str1;
$Str1="";
$HaveValue=1;
}
// How many tens?
$Tens=floor($n/10);
if ($Tens)
$n=$n-(10*$Tens);
// How many ones?
$Ones=floor($n/1);
if ($Ones)
$n=$n-($Ones);
// Anything after the decimal point?
$dogdoo = strpos($number,".");
if ($dogdoo !== FALSE)
$Point=substr($number, -(strlen($number) - strrpos($number, ".") - 1) );
// If 1-9
$Str1="";
if ($Tens == 0)
{
if ($Ones == 0)
{
if (!$HaveValue)
$Str1=$StrsA[0];
}
else
// 1 is in 2, 2 is in 3, etc
$Str1=$StrsA[$Ones+1];
}
else if ($Tens == 1)
// If 10-19
{
// 10 is in 11, 11 is in 12, etc
$Str1=$StrsA[$Ones+11];
}
else
{
// 20 is in 21, 30 is in 31, etc
$Str1=$StrsA[($Tens*10)+1];
// Get "ones" portion
if ($Ones)
$Str2=NumberAsString($Ones);
$Str1=$Str1.$Strs["space"].$Str2;
}
// Build result
if (strlen($Str1))
{
if (strlen($Result))
$Result=$Result.$Strs["space"].$Strs["and"].$Strs["space"];
$Result=$Result.$Str1;
}
// Is there a decimal point to get?
if ($Point)
{
$Str2=NumberAsString($Point);
$Result=$Result.$Strs["space"].$Strs["point"].$Strs["space"].$Str2;
}
return $Result;
}
?>- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
On similar lines, i wrote this to convert the datetime output you get from mysql (DD-MM-YY HH:MM:SS) into something more readable.
Eg. 20-03-05 21:03:04 becomes 20th March 05 21:03:04
I wrote this a while back though. As you can see I wasn't using assoicative arrays back then but it works well enough.
Eg. 20-03-05 21:03:04 becomes 20th March 05 21:03:04
Code: Select all
function formatted_sql_datetime($datetime) {
$datetime = explode(" ",$posted); // split the date from the time
$datepart = explode("-",$datetime[0]); // split the date into D,M and Y
$timepart = explode(":",$datetime[1]); // split the time into HH MM SS
echo $datepart[2];
switch ($datepart[2]) { // the days
case "11": echo "th"; break; // 11th and
case "12": echo "th"; break; // 12th are exceptions to
default : { // this rule
switch ($datepart[2][1]) { // that says all numbers ending in
case "1" : echo "st"; break; // 1 have 'st' suffix
case "2" : echo "nd"; break; // 2 have 'nd' suffix
default : echo "th"; // and everything else has 'th' suffix
}
}
}
echo " "; // leave a gap
switch ($datepart[1]) { // months written up nicely
case "01" : echo "January"; break;
case "02" : echo "February"; break;
case "03" : echo "March"; break;
case "04" : echo "April"; break;
case "05" : echo "May"; break;
case "06" : echo "June"; break;
case "07" : echo "July"; break;
case "08" : echo "August"; break;
case "09" : echo "September"; break;
case "10" : echo "October"; break;
case "11" : echo "November"; break;
case "12" : echo "December"; break;
default : echo "{bad month}"; // in case its 00 or greater than 12
}
echo " " . $datepart[0] . " at " . ($timepart[0])%24 . ":" . $timepart[1] . ":" . $timepart[2];
}for date, it's easiest to use the date() function of php
this will look like May 25, 2005 03:13:20
Code: Select all
<?php
$date=date('M d, Y H:i:s');
echo 'this will look like <b>'.$date.'</b>';
?>- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
The following functions allow you to get localized info, instead of hardcoding them 
http://wwW.php.net/setlocale
http://www.php.net/localeconv
http://wwW.php.net/setlocale
http://www.php.net/localeconv