CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Numbers to Words

Print topic Send  topic

Author Message
lvalverdeb

Posts: 299
Posted: 01/21/2004, 3:09 PM

Just wanted to share a php version of a number to words converter originally written in VFP by Dimple Patel. The function converts any number up to 99 Billions. For example: num2words(4500.50) returns Four Thousand Five Hundred and 50/100. I use it to produce accounting vouchers and other accounting documents. Hope you find it useful.

function num2words($amount,$currency="USD")
{
$words = "Zero";
$amount = doubleval($amount);
if ($amount < 0)
$amount *=-1; // accounting apps use negative figures as credits
if ($amount == 0)
return $words;
$words = rtrim(getWordStr($amount,0));
$cents = ($amount-intval($amount))*100;
if ($cents == 0)
$cents = "00";
$words.=" And ".$cents."/100";
if (isset($currency))
$words .= " ".$currency;
return $words;
}

function getWordStr($amt,$commaindex)
{
$word1to10 = array("","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten");
$word11to20 = array("","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty");
$word10to90 = array("","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety");
$keywords = array("","Hundred","Thousand","Million","Billion","Trillion");
$commagap = "21333"; // right to left comma gap i.e. 5,234,798,2,34 $
$lencomma = 4;
$rightStr = "";
$leftStr = "";
if ($amt <= 0)
return $rightStr;
$indice = intval(substr($commagap,$commaindex,1));
$lnDivisor = pow(10,$indice);
$lnRemainder = intval($amt % $lnDivisor);
$amt = intval($amt/$lnDivisor);
if ($lnRemainder > 0 && $lnRemainder < 100)
{
switch ($lnRemainder)
{
case 0:
$rightStr = "";
break;
case ($lnRemainder <=10):
$rightStr = $word1to10[$lnRemainder];
break;
case ($lnRemainder <= 20):
$rightStr = $word11to20[$lnRemainder-10];
break;
case ($lnRemainder > 20):
$rightStr = $word10to90[intval($lnRemainder/10)];
$lnRemainder %= 10;
if ($lnRemainder != 0)
$rightStr .= " ".$word1to10[$lnRemainder];
break;
}
}
else { $rightStr = getWordStr($lnRemainder,0);}
if ($commaindex < $lencomma)
{
$leftStr = getWordStr($amt,$commaindex+1);
} else {$leftStr = getWordStr($amt,0); }

if ($commaindex < $lencomma)
{
if (($amt % pow(10,intval(substr($commagap,$commaindex+1,1)))) > 0)
{
if (strlen($leftStr))
{
$leftStr .= " ".$keywords[$commaindex+1];
}
}
} else {
if(strlen($leftStr))
{
$leftStr .= " ".$keywords[$commaindex+1];
}
}
unset($word1to10);
unset($word11to20);
unset($word10to90);
unset($keywords);
if (strlen($leftStr))
{
return rtrim($leftStr)." ".$rightStr;
} else {return rtrim($leftStr).$rightStr;}
}
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message
Walter Kempees
Posted: 01/26/2004, 1:34 AM

Thanks for sharing.
Walt
"lvalverdeb" <lvalverdeb@forum.codecharge> schreef in bericht
news:5400f06a757ddb@news.codecharge.com...
> Just wanted to share a php version of a number to words converter
originally written in VFP by Dimple Patel. The function converts any number
up to 99 Billions. For example: num2words(4500.50) returns Four Thousand
Five Hundred and 50/100. I use it to produce accounting vouchers and other
accounting documents. Hope you find it useful.
>
> function num2words($amount,$currency="USD")
> {
> $words = "Zero";
> $amount = doubleval($amount);
> if ($amount < 0)
> $amount *=-1; // accounting apps use negative figures as credits
> if ($amount == 0)
> return $words;
> $words = rtrim(getWordStr($amount,0));
> $cents = ($amount-intval($amount))*100;
> if ($cents == 0)
> $cents = "00";
> $words.=" And ".$cents."/100";
> if (isset($currency))
> $words .= " ".$currency;
> return $words;
> }
>
> function getWordStr($amt,$commaindex)
> {
> $word1to10 =
array("","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten
");
> $word11to20 =
array("","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Sevent
een","Eighteen","Nineteen","Twenty");
> $word10to90 =
array("","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty",
"Ninety");
> $keywords =
array("","Hundred","Thousand","Million","Billion","Trillion");
> $commagap = "21333"; // right to left comma gap i.e.
5,234,798,2,34 $
> $lencomma = 4;
> $rightStr = "";
> $leftStr = "";
> if ($amt <= 0)
> return $rightStr;
> $indice = intval(substr($commagap,$commaindex,1));
> $lnDivisor = pow(10,$indice);
> $lnRemainder = intval($amt % $lnDivisor);
> $amt = intval($amt/$lnDivisor);
> if ($lnRemainder > 0 && $lnRemainder < 100)
> {
> switch ($lnRemainder)
> {
> case 0:
> $rightStr = "";
> break;
> case ($lnRemainder <=10):
> $rightStr = $word1to10[$lnRemainder];
> break;
> case ($lnRemainder <= 20):
> $rightStr = $word11to20[$lnRemainder-10];
> break;
> case ($lnRemainder > 20):
> $rightStr = $word10to90[intval($lnRemainder/10)];
> $lnRemainder %= 10;
> if ($lnRemainder != 0)
> $rightStr .= " ".$word1to10[$lnRemainder];
> break;
> }
> }
> else { $rightStr = getWordStr($lnRemainder,0);}
> if ($commaindex < $lencomma)
> {
> $leftStr = getWordStr($amt,$commaindex+1);
> } else {$leftStr = getWordStr($amt,0); }
>
> if ($commaindex < $lencomma)
> {
> if (($amt % pow(10,intval(substr($commagap,$commaindex+1,1)))) > 0)
> {
> if (strlen($leftStr))
> {
> $leftStr .= " ".$keywords[$commaindex+1];
> }
> }
> } else {
> if(strlen($leftStr))
> {
> $leftStr .= " ".$keywords[$commaindex+1];
> }
> }
> unset($word1to10);
> unset($word11to20);
> unset($word10to90);
> unset($keywords);
> if (strlen($leftStr))
> {
> return rtrim($leftStr)." ".$rightStr;
> } else {return rtrim($leftStr).$rightStr;}
> }
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

Nicole

Posts: 586
Posted: 01/26/2004, 5:35 AM

Guys, please use the Code tags
your PHP code[/Code]
when you paste code snippets. The post is unreadable now.

_________________
Regards,
Nicole
View profile  Send private message
lvalverdeb

Posts: 299
Posted: 01/26/2004, 6:39 AM

Posting again with
 tags.    
  
  
function num2words($amount,$currency="USD")  
{  
	$words = "Zero";  
	$amount = doubleval($amount);  
	if ($amount < 0)  
		$amount *=-1; // accounting apps use negative figures as credits  
	if ($amount == 0)  
		return $words;  
	$words = rtrim(getWordStr($amount,0));  
	$cents = ($amount-intval($amount))*100;  
	if ($cents == 0)  
		$cents = "00";  
	$words.=" And ".$cents."/100";  
	if (isset($currency))  
		$words .= " ".$currency;  
	return $words;  
}  
  
function getWordStr($amt,$commaindex)  
{  
	$word1to10  = array("","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten");  
	$word11to20 = array("","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty");  
	$word10to90 = array("","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety");   
	$keywords   = array("","Hundred","Thousand","Million","Billion","Trillion");  
	$commagap 	= "21333";  //         right to left comma gap i.e. 5,234,798,2,34 $  
	$lencomma 	= 4;  
	$rightStr = "";  
	$leftStr = "";  
	if ($amt <= 0)  
		return $rightStr;  
	$indice = intval(substr($commagap,$commaindex,1));  
	$lnDivisor = pow(10,$indice);  
	$lnRemainder = intval($amt % $lnDivisor);  
	$amt = intval($amt/$lnDivisor);  
	if ($lnRemainder > 0 && $lnRemainder < 100)  
	{  
		switch ($lnRemainder)  
		{  
			case 0:  
				$rightStr = "";  
				break;  
			case ($lnRemainder <=10):  
				$rightStr = $word1to10[$lnRemainder];  
				break;  
			case ($lnRemainder <= 20):  
				$rightStr = $word11to20[$lnRemainder-10];  
				break;  
			case ($lnRemainder > 20):  
				$rightStr = $word10to90[intval($lnRemainder/10)];  
				$lnRemainder %= 10;  
				if ($lnRemainder != 0)  
					$rightStr .= " ".$word1to10[$lnRemainder];  
				break;  
		}  
	}   
	else { $rightStr = getWordStr($lnRemainder,0);}  
	if ($commaindex < $lencomma)  
	{  
		$leftStr = getWordStr($amt,$commaindex+1);  
	} else	{$leftStr = getWordStr($amt,0);	}  
	  
	if ($commaindex < $lencomma)  
	{  
		if (($amt % pow(10,intval(substr($commagap,$commaindex+1,1)))) > 0)   
		{  
			if (strlen($leftStr))  
			{  
				$leftStr .= " ".$keywords[$commaindex+1];  	  
			}  
		}  
	} else {  
			if(strlen($leftStr))   
			{   
				$leftStr .= " ".$keywords[$commaindex+1];  
			}  
		}  
	unset($word1to10);  
	unset($word11to20);  
	unset($word10to90);  
	unset($keywords);  
	if (strlen($leftStr))  
	{  
		return rtrim($leftStr)."  ".$rightStr;  
	} else {return rtrim($leftStr).$rightStr;}  
}  

_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright 2003-2004 by UltraApps.com  and YesSoftware, Inc.