random1 Posted March 1, 2010 Share Posted March 1, 2010 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 More sharing options...
ram4nd Posted March 1, 2010 Share Posted March 1, 2010 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. Link to comment https://forums.phpfreaks.com/topic/193737-currency-money-to-words-conversion/#findComment-1019860 Share on other sites More sharing options...
random1 Posted March 1, 2010 Author Share Posted March 1, 2010 That right it's been modified from a intToWords() function. Link to comment https://forums.phpfreaks.com/topic/193737-currency-money-to-words-conversion/#findComment-1020099 Share on other sites More sharing options...
JAY6390 Posted March 1, 2010 Share Posted March 1, 2010 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; } Link to comment https://forums.phpfreaks.com/topic/193737-currency-money-to-words-conversion/#findComment-1020105 Share on other sites More sharing options...
random1 Posted March 2, 2010 Author Share Posted March 2, 2010 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); Link to comment https://forums.phpfreaks.com/topic/193737-currency-money-to-words-conversion/#findComment-1020350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.