Page 1 of 2

Convert Number to Words

Posted: Mon Feb 26, 2007 12:34 am
by neel_basu
This programm Will Convert numbers to words i've relesed it here for testing purpouse. i am making a framework and its a prt of this. Please test it and tell me weather there is a bug or not . thank you.

Code: Select all

<?php
/*************************************************************************************
** Copyright 2007 Neel Basu <neel.basu.z@gmail.com> Some rights Reserved 						**
--------------------------------------------------------------------------------------
This File Has Been Reliesed Just for Testing Purpouse It Will be Bound With the framework
After Finishing . 
--------------------------------------------------------------------------------------
You can use it as long as the above message is intact
**************************************************************************************/
header("Content-Type: text/plain");
function dict($get)
	{
		$num[1] = "One";
		$num[2] = "Two";
		$num[3] = "Three";
		$num[4] = "Four";
		$num[5] = "Five";
		$num[6] = "Six";
		$num[7] = "Seven";
		$num[8] = "Eight";
		$num[9] = "Nine";
		$num[10] = "Ten";
		$num[11] = "Eleven";
		$num[12] = "Twelve";
		$num[13] = "Thirteen";
		$num[14] = "Fourteen";
		$num[15] = "Fiveteen";
		$num[16] = "Sixteen";
		$num[17] = "Seventeen";
		$num[18] = "Eighteen";
		$num[19] = "Nineteen";
		$num[20] = "Twenty";
		$num[30] = "Thirty";
		$num[40] = "Fourty";
		$num[50] = "Fivety";
		$num[60] = "Sixty";
		$num[70] = "Seventy";
		$num[80] = "Eighty";
		$num[90] = "Ninety";
		//$num["t"] = "ty";
		$num["hund"] = "Hundreed";
		$num["thsnd"] = "Thousand";
		$num["mill"] = "Million";
		$num["bill"] = "Billion";
		$num["trill"] = "Billion";
		$num["blank"] = "";
		$num[00] = "";
		$num[01] = "One";
		$num[02] = "Two";
		$num[03] = "Three";
		$num[04] = "Four";
		$num[05] = "Five";
		$num[06] = "Six";
		$num[07] = "Seven";
		$num[08] = "Eight";
		$num[09] = "Nine";
		$num[0] = "Zero";
		return $num[$get];
	}
function dict_pos($get)
	{
		//$num[0] = "Hundreed";
		$num[0] = "Thousand";
		$num[1] = "Million";
		$num[2] = "Billion";
		$num[3] = "Trillion";
		$num[4] = "Quadrillion";
		$num[5] = "Quintillion";
		$num[6] = "Sextillion";
		$num[7] = "Octillion";
		$num[8] = "Nonillion";
		$num[9] = "Decillion";
		$num[10] = "Undecillion";
		$num[11] = "Duodecillion";
		$num[12] = "Tredecillion";
		$num[13] = "Quattuordecillion";
		$num[14] = "Quindecillion";
		$num[15] = "Sexdecillion";
		$num[16] = "Septendecillion";
		$num[17] = "Octodecillion";
		$num[18] = "Novemdecillion";
		$num[19] = "Vigintillion";
		$num[20] = "Centillion";
		return $num[$get];
	}
function decade($num_str)
	{
		$to_str = (string)($num_str);
		if($to_str[1] == 0)
			{
				if($to_str >= 10)
					{
						return dict($to_str[0]*10);
					}
				else 
					{
						return dict($to_str[0]);
					}
			}
		else 
			{
				if($num_str >= 11 && $num_str <= 19)
					{
						return dict($num_str);
					}
				else 
					{
						return dict($to_str[0]*10)."-".dict($to_str[1]);
					}
			}
	}
function hundreed($num_str)
	{
		$to_str = (string)($num_str);
		if($num_str >= 0 && $num_str <= 10)
			{
				return dict($to_str);
			}
		if($num_str >= 11 && $num_str <= 99)
			{
				return decade($to_str);
			}
		if($num_str >= 100 && $num_str <= 999)
			{
				$dec = (int)($to_str[1].$to_str[2]);
				if($dec != 0)
					{
						return dict($to_str[0])." ".dict("hund")." ".decade($dec);
					}
				else 
					{
						return dict($to_str[0])." ".dict("hund");
					}
			}
	}
function to_word($get_int)
	{
		$to_str = (string)$get_int;
		$len = strlen($to_str)-1;
		for($i=$len;$i>=0;$i-=3)
			{
				$part[2] = $to_str[$i];
				$part[1] = $to_str[$i-1];
				$part[0] = $to_str[$i-2];				
				if($part[0] != '0')
					{
						$hnd = $part[0].$part[1].$part[2];
					}
				else 
					{
						if($part[1] != '0')
							{
								$hnd = $part[1].$part[2];
							}
						else 
							{
								$hnd = $part[2];
							}
					}				
				$num_r[] = hundreed($hnd);
			}
		for($i=count($num_r)-1;$i>=0;$i--)
			{
				if($i != (count($num_r)-1))
					{
						$ret[] = dict_pos($i);
					}
				$ret[] = $num_r[$i];
			}
		foreach($ret as $key => $val)
			{
				$return .= $val;
				if($key != (count($ret)-1))
					{
						$return .= " ";
					}
			}
		for($i=0;$i<=21;$i++)
			{
				$return = str_replace("Zero ".dict_pos($i)." ", "", $return);
			}
		return $return;
	}
?>
<?php
$num = "999999999999999999999999999999999999999999999999999999999999999999";
echo $num."\n".to_word($num);
?>
Browser Output wrote:999999999999999999999999999999999999999999999999999999999999999999
Nine Hundreed Ninety-Nine Centillion Nine Hundreed Ninety-Nine Vigintillion Nine Hundreed Ninety-Nine Novemdecillion Nine Hundreed Ninety-Nine Octodecillion Nine Hundreed Ninety-Nine Septendecillion Nine Hundreed Ninety-Nine Sexdecillion Nine Hundreed Ninety-Nine Quindecillion Nine Hundreed Ninety-Nine Quattuordecillion Nine Hundreed Ninety-Nine Tredecillion Nine Hundreed Ninety-Nine Duodecillion Nine Hundreed Ninety-Nine Undecillion Nine Hundreed Ninety-Nine Decillion Nine Hundreed Ninety-Nine Nonillion Nine Hundreed Ninety-Nine Octillion Nine Hundreed Ninety-Nine Sextillion Nine Hundreed Ninety-Nine Quintillion Nine Hundreed Ninety-Nine Quadrillion Nine Hundreed Ninety-Nine Trillion Nine Hundreed Ninety-Nine Billion Nine Hundreed Ninety-Nine Million Nine Hundreed Ninety-Nine Thousand Nine Hundreed Ninety-Nine
Numbers More than Billion Should be in " " or ' ' Numbers such as 2005 doesn't needs any " or '

Posted: Mon Feb 26, 2007 3:08 am
by Kieran Huggins
Not too bad at all!

There's might be a l10n issue though, as some places (including N.America) use the spoken syntax "Nine Hundred and Ninety-Nine" for the join within the comma spaces... if that even makes sense.... example:

999,999,999 = "Nine Hundreed and Ninety-Nine Million, Nine Hundreed and Ninety-Nine Thousand, Nine Hundreed and Ninety-Nine"

I believe the UK and most of the EU does it your way, but I could be wrong.

Posted: Mon Feb 26, 2007 4:32 am
by Jenk
UK use "and"

"One hundred and eighty."

Posted: Mon Feb 26, 2007 4:52 am
by neel_basu
I havn't Used "and" Cause Google Doesn't Use It ( http://www.google.com/search?hl=en&clie ... tnG=Search ). So I thought "and" is not International
There's might be a l10n issue though,
Sorry I didn't understand

Posted: Mon Feb 26, 2007 5:07 am
by Chris Corbyn
I don't see the "and" being an issue, and perhaps not too difficult to specify a separator neither :)

Posted: Mon Feb 26, 2007 5:12 am
by neel_basu
I've to change the

Code: Select all

return dict($to_str[0])." ".dict("hund")." ".decade($dec);
to

Code: Select all

return dict($to_str[0])." ".dict("hund")." and ".decade($dec);
There's might be a l10n issue though,
I didn't understand it

Posted: Mon Feb 26, 2007 6:15 am
by Kieran Huggins
neel_basu wrote:
There's might be a l10n issue though,
I didn't understand it
l10n = "localization"

http://en.wikipedia.org/wiki/L10n

Posted: Mon Feb 26, 2007 6:57 am
by Kadanis
the only thing i noticed was a small error in one of the functions

Code: Select all

function dict_pos($get)
        {
                //$num[0] = "Hundreed";
                $num[0] = "Thousand";
                $num[1] = "Million";
                $num[2] = "Billion";
                $num[3] = "Trillion";
                $num[4] = "Quadrillion";
                $num[5] = "Quintillion";
                $num[6] = "Sextillion";
                $num[7] = "Octillion";
                $num[8] = "Nonillion";
                $num[9] = "Decillion";
                $num[10] = "Undecillion";
                $num[11] = "Duodecillion";
                $num[12] = "Tredecillion";
                $num[13] = "Quattuordecillion";
                $num[14] = "Quindecillion";
                $num[15] = "Sexdecillion";
                $num[16] = "Septendecillion";
                $num[17] = "Octodecillion";
                $num[18] = "Novemdecillion";
                $num[19] = "Vigintillion";
                $num[20] = "Centillion";
                return $num[$get];
        }
you've dropped out of synch here a little.

Code: Select all

$num[7] = "Octillion";
should be

Code: Select all

$num[7] = "Septillion";
this has thrown out all subsequent assignments.

Posted: Mon Feb 26, 2007 7:21 am
by neel_basu
Do You mean
Septillion should be inserted between Sextillion and Octillion

Posted: Mon Feb 26, 2007 7:25 am
by nimmØ2
Yesh. :)

Also everything below needs to be bumped one down, so octillion corresponds with '8' and etc.

Posted: Mon Feb 26, 2007 7:32 am
by neel_basu
But Does the word "Septillion" exists or it would be "Heptillion"

Posted: Mon Feb 26, 2007 8:36 am
by Kadanis
well according to the Wikitionary

http://en.wiktionary.org/wiki/septillion

From the prefix septi- (seven) + -illion (from million); ie the seventh power of a million, 1042.


Heptillion - Not Found.

Don't know if that helps ;) it might just mean no one has added it to the Wiki.

and yes, you need to fix the code like this

Code: Select all

function dict_pos($get)
        {
                //$num[0] = "Hundreed";
                $num[0] = "Thousand";
                $num[1] = "Million";
                $num[2] = "Billion";
                $num[3] = "Trillion";
                $num[4] = "Quadrillion";
                $num[5] = "Quintillion";
                $num[6] = "Sextillion";
                $num[7] = "Septillion";
                $num[8] = "Octillion";
                $num[9] = "Nonillion";
                $num[10] = "Decillion";
                $num[11] = "Undecillion";
                $num[12] = "Duodecillion";
                $num[13] = "Tredecillion";
                $num[14] = "Quattuordecillion";
                $num[15] = "Quindecillion";
                $num[16] = "Sexdecillion";
                $num[17] = "Septendecillion";
                $num[18] = "Octodecillion";
                $num[19] = "Novemdecillion";
                $num[20] = "Vigintillion";
                return $num[$get];
        }

Posted: Mon Feb 26, 2007 9:12 am
by neel_basu
OK Thanks i'll fix it . e.g. replace this dict_pos() by the previous
http://www.unc.edu/~rowlett/units/large.html this link is useful

Posted: Mon Feb 26, 2007 9:40 am
by Kieran Huggins
it's a good thing we got THAT cleared up... :rofl:

Is there a Septillion of anything? Let alone a Vigintillion?

Don't get me wrong - I do think it's good to be prepared, but I just thought it was funny ;-)

Posted: Mon Feb 26, 2007 9:52 am
by Kadanis
who knows ;)

maybe the code is for a mathematics site :lol:

i certainly wouldn't want to have to handle numbers that size on a frequent basis.