Jump to content

Currency (Money) To Words Conversion?


random1

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/193737-currency-money-to-words-conversion/
Share on other sites

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;
}

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.