Jump to content


Photo

Currency (Money) To Words Conversion?


  • Please log in to reply
4 replies to this topic

#1 random1

random1

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 28 February 2010 - 11:10 PM

Hey All,

I'm creating a currency (money) to words function:

	function convertCurrencyToWords($x)
	{

	$nwords = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 
					'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 
					'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 
					'nineteen', 'twenty', 30 => 'thirty', 40 => 'forty', 
					50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 
					90 => 'ninety',
					'dollars' => 'dollars', 'cents' => 'cents');

		if(!is_float($x) && !is_numeric($x))
		{
			$w = '#';
		}
		else
		{
			if($x < 0)
			{
				$w = 'minus '; 
				$x = -$x; 
			}
			else
			{
				$w = ''; 
			}
			if($x < 21)
			{
				$w .= $nwords[$x];
			}
			else if($x < 100)
			{
				$w .= $nwords[10 * floor($x/10)];
				$r = fmod($x, 10);
				if($r > 0)
				{
					$w .= '-'. $nwords[$r];
				}
				
				/*if(is_float($x))
				{
					$w .= ' ' . $nwords['cents'];
				}
				else if(is_int($x))
				{
					$w .= ' ' . $nwords['dollars'];
				}*/
			}
			else if($x < 1000)
			{
				$w .= $nwords[floor($x/100)] .' hundred';
				$r = fmod($x, 100);
				if($r > 0)
				{
					$w .= ' and '. $this->convertCurrencyToWords($r);
				}
			}
			else if($x < 1000000)
			{
				$w .= $this->convertCurrencyToWords(floor($x/1000)) .' thousand';
				$r = fmod($x, 1000);
				if($r > 0)
				{
					$w .= ' '; 
					if($r < 100)
					{
						$w .= 'and'; 
					}
					$w .= $this->convertCurrencyToWords($r); 
				}
			}
			else
			{
				$w .= $this->convertCurrencyToWords(floor($x/1000000)) .' million'; 
				$r = fmod($x, 1000000);
				if($r > 0)
				{ 
					$w .= ' ';
					if($r < 100)
					{
						$word .= 'and ';
					}
					$w .= $this->convertCurrencyToWords($r);
				}
			}
		}
		return $w; 
	}

Example call:
convertCurrencyToWords(3782.11);

Current output is:

three thousand seven hundred and eighty-two cents

Should be:

three thousand seven hundred and eighty-two dollars and 11 cents

Any ideas for how I can change it to recognize dollars or cents?

#2 ram4nd

ram4nd

    Advanced Member

  • Members
  • PipPipPip
  • 461 posts
  • LocationEstonia

Posted 01 March 2010 - 09:50 AM

I bet you haven't done that yourself, so you can just find better one from internet, or if you want to improve this one then look for . in string and, separate it to 2 integers.

#3 random1

random1

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 01 March 2010 - 04:38 PM

That right it's been modified from a intToWords() function.

#4 JAY6390

JAY6390

    Advanced Member

  • Members
  • PipPipPip
  • 825 posts
  • LocationUK

Posted 01 March 2010 - 04:47 PM

You should get the intToWords() function and leave it as it is, then extend upon it with your function

(Untested but something like this)

function convertCurrencyToWords($number) {
    if(!is_numeric($number)) return false;
    $nums = explode('.', $number);
    $out = intToWords($nums[0]).' dollars';
    if(isset($nums[1])) {
        $out .= ' and '.intToWords($nums[1]).' cents';
    }
    return $out;
}


#5 random1

random1

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 02 March 2010 - 06:22 AM

Thanks,

I ended up with:

	function convertIntegerToWords($x)
	{

	$nwords = array(    'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 
	                     'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 
	                     'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 
	                     'nineteen', 'twenty', 30 => 'thirty', 40 => 'forty', 
	                     50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 
	                     90 => 'ninety' );

	     if(!is_numeric($x)) 
	     { 
	         $w = '#'; 
	     }else if(fmod($x, 1) != 0) 
	     { 
	         $w = '#'; 
	     }else{ 
	         if($x < 0) 
	         { 
	             $w = 'minus '; 
	             $x = -$x; 
	         }else{ 
	             $w = ''; 
	         } 
	         if($x < 21) 
	         { 
	             $w .= $nwords[$x]; 
	         }else if($x < 100) 
	         { 
	             $w .= $nwords[10 * floor($x/10)]; 
	             $r = fmod($x, 10); 
	             if($r > 0) 
	             { 
	                 $w .= '-'. $nwords[$r]; 
	             } 
	         } else if($x < 1000) 
	         { 
	             $w .= $nwords[floor($x/100)] .' hundred'; 
	             $r = fmod($x, 100); 
	             if($r > 0) 
	             { 
	                 $w .= ' and '. $this->convertIntegerToWords($r); 
	             } 
	         } else if($x < 1000000) 
	         { 
	             $w .= $this->convertIntegerToWords(floor($x/1000)) .' thousand'; 
	             $r = fmod($x, 1000); 
	             if($r > 0) 
	             { 
	                 $w .= ' '; 
	                 if($r < 100) 
	                 { 
	                     $w .= 'and'; 
	                 } 
	                 $w .= $this->convertIntegerToWords($r); 
	             } 
	         } else { 
	             $w .= $this->convertIntegerToWords(floor($x/1000000)) .' million'; 
	             $r = fmod($x, 1000000); 
	             if($r > 0) 
	             { 
	                 $w .= ' '; 
	                 if($r < 100) 
	                 { 
	                     $word .= 'and '; 
	                 } 
	                 $w .= $this->convertIntegerToWords($r); 
	             } 
	         } 
	     } 
	     return $w; 
	}

	function convertCurrencyToWords($number)
	{
		$currencylabelsarray = array('dollars' => 'dollars', 'cents' => 'cents');
		if(!is_numeric($number)) return false;
		$nums = explode('.', $number);
		$out = $this->convertIntegerToWords($nums[0]) . ' dollars';
		if(isset($nums[1])) {
		$out .= ' and ' . $this->convertIntegerToWords($nums[1]) .' cents';
		}
		return $out;
	}

and calling:

convertCurrencyToWords(324.17);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com