/** * Converts a currency value to word representation (1.02 => one dollar two cents) * If the number has not any fraction part, the "cents" number is omitted. * * @param float $num A float/integer/string number representing currency value * * @param string $locale Language name abbreviation. Optional. Defaults to en_US. * * @param string $int_curr International currency symbol * as defined by the ISO 4217 standard (three characters). * E.g. 'EUR', 'USD', 'PLN'. Optional. * Defaults to $def_currency defined in the language class. * * @return string The corresponding word representation * * @access public * @author Piotr Klaban <*****@*****.**> * @since PHP 4.2.3 */ function toCurrency($num, $locale = 'en_US', $int_curr = '') { $ret = $num; @(include_once "Numbers/Words/lang.{$locale}.php"); $classname = "Numbers_Words_{$locale}"; if (!class_exists($classname)) { return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.{$locale}.php file"); } $methods = get_class_methods($classname); if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) { return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '{$classname}' class"); } @($obj = new $classname()); if (strpos($num, '.') === false) { $ret = trim($obj->toCurrencyWords($int_curr, $num)); } else { $currency = explode('.', $num, 2); /* add leading zero */ if (strlen($currency[1]) == 1) { $currency[1] .= '0'; } $ret = trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1])); } return $ret; }
/** * Converts a currency value to its word representation * (with monetary units) in Portuguese Brazilian * * @param integer $int_curr An international currency symbol * as defined by the ISO 4217 standard (three characters) * @param integer $decimal A money total amount without fraction part (e.g. amount of dollars) * @param integer $fraction Fractional part of the money amount (e.g. amount of cents) * Optional. Defaults to false. * @param integer $convert_fraction Convert fraction to words (left as numeric if set to false). * Optional. Defaults to true. * * @return string The corresponding word representation for the currency * * @access public * @author Igor Feghali <*****@*****.**> * @since Numbers_Words 0.11.0 */ public function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fraction = true) { $neg = 0; $ret = array(); $nodec = false; /** * Negative ? * We can lose the '-' sign if we do the * check after number_format() call (i.e. -0.01) */ if (substr($decimal, 0, 1) == '-') { $decimal = -$decimal; $neg = 1; } /** * Removes leading zeros, spaces, decimals etc. * Adds thousands separator. */ $num = number_format($decimal, 0, '', ''); /** * Checking if given currency exists in driver. * If not, use default currency */ $int_curr = strtoupper($int_curr); if (!isset($this->_currency_name[$int_curr])) { $int_curr = $this->def_currency; } /** * Currency names and plural */ $curr_names = $this->_currency_names[$int_curr]; if ($num > 0) { /** * Word representation of decimal */ $ret[] = $this->toWords($num); /** * Special case. */ if (substr($num, -6) == '000000') { $ret[] = trim($this->_curr_sep); } /** * Testing plural. Adding currency name */ if ($num > 1) { $ret[] = $curr_names[0][1]; } else { $ret[] = $curr_names[0][0]; } } /** * Test if fraction was given and != 0 */ $fraction = (int) $fraction; if ($fraction) { /** * Removes leading zeros, spaces, decimals etc. * Adds thousands separator. */ $num = number_format($fraction, 0, '.', '.'); /** * Testing Range */ if ($num < 0 || $num > 99) { return Numbers_Words::raiseError('Fraction out of range.'); } /** * Have we got decimal? */ if (count($ret)) { $ret[] = trim($this->_sep); } else { $nodec = true; } /** * Word representation of fraction */ if ($convert_fraction) { $ret[] = $this->toWords($num); } else { $ret[] = $num; } /** * Testing plural */ if ($num > 1) { $ret[] = $curr_names[1][1]; } else { $ret[] = $curr_names[1][0]; } /** * Have we got decimal? * If not, include currency name after cents. */ if ($nodec) { $ret[] = trim($this->_curr_sep); $ret[] = $curr_names[0][0]; } } /** * Negative ? */ if ($neg) { $ret[] = $this->_minus; } return implode(' ', $ret); }
/** * Converts a currency value to word representation (1.02 => one dollar two cents) * If the number has not any fraction part, the "cents" number is omitted. * * @param float $num A float/integer/string number representing currency value * * @param string $locale Language name abbreviation. Optional. Defaults to en_US. * * @param string $int_curr International currency symbol * as defined by the ISO 4217 standard (three characters). * E.g. 'EUR', 'USD', 'PLN'. Optional. * Defaults to $def_currency defined in the language class. * * @return string The corresponding word representation * * @access public * @author Piotr Klaban <*****@*****.**> * @since PHP 4.2.3 * @return string */ function toCurrency($num, $locale = 'en_US', $int_curr = '') { $ret = $num; @(include_once "Numbers/Words/lang.{$locale}.php"); $classname = "Numbers_Words_{$locale}"; if (!class_exists($classname)) { return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.{$locale}.php file"); } $methods = get_class_methods($classname); if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) { return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '{$classname}' class"); } @($obj = new $classname()); // round if a float is passed, use Math_BigInteger otherwise if (is_float($num)) { $num = round($num, 2); } if (strpos($num, '.') === false) { return trim($obj->toCurrencyWords($int_curr, $num)); } $currency = explode('.', $num, 2); $len = strlen($currency[1]); if ($len == 1) { // add leading zero $currency[1] .= '0'; } elseif ($len > 2) { // get the 3rd digit after the comma $round_digit = substr($currency[1], 2, 1); // cut everything after the 2nd digit $currency[1] = substr($currency[1], 0, 2); if ($round_digit >= 5) { // round up without losing precision include_once "Math/BigInteger.php"; $int = new Math_BigInteger(join($currency)); $int = $int->add(new Math_BigInteger(1)); $int_str = $int->toString(); $currency[0] = substr($int_str, 0, -2); $currency[1] = substr($int_str, -2); // check if the rounded decimal part became zero if ($currency[1] == '00') { $currency[1] = false; } } } return trim($obj->toCurrencyWords($int_curr, $currency[0], $currency[1])); }