/** * DOLLAR * * This function converts a number to text using currency format, with the decimals rounded to the specified place. * The format used is $#,##0.00_);($#,##0.00).. * * @param float $value The value to format * @param int $decimals The number of digits to display to the right of the decimal point. * If decimals is negative, number is rounded to the left of the decimal point. * If you omit decimals, it is assumed to be 2 * @return string */ public static function DOLLAR($value = 0, $decimals = 2) { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $decimals = is_null($decimals) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($decimals); // Validate parameters if (!is_numeric($value) || !is_numeric($decimals)) { return PHPExcel_Calculation_Functions::NaN(); } $decimals = floor($decimals); if ($decimals > 0) { return money_format('%.' . $decimals . 'n', $value); } else { $round = pow(10, abs($decimals)); if ($value < 0) { $round = 0 - $round; } $value = PHPExcel_Calculation_MathTrig::MROUND($value, $round); // The implementation of money_format used if the standard PHP function is not available can't handle decimal places of 0, // so we display to 1 dp and chop off that character and the decimal separator using substr return substr(money_format('%.1n', $value), 0, -2); } }
/** * DOLLAR * * This function converts a number to text using currency format, with the decimals rounded to the specified place. * The format used is $#,##0.00_);($#,##0.00).. * * @param float $value The value to format * @param int $decimals The number of digits to display to the right of the decimal point. * If decimals is negative, number is rounded to the left of the decimal point. * If you omit decimals, it is assumed to be 2 * @return string */ public static function DOLLAR($value = 0, $decimals = 2) { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $decimals = is_null($decimals) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($decimals); // Validate parameters if (!is_numeric($value) || !is_numeric($decimals)) { return PHPExcel_Calculation_Functions::NaN(); } $decimals = floor($decimals); $mask = '$#,##0'; if ($decimals > 0) { $mask .= '.' . str_repeat('0', $decimals); } else { $round = pow(10, abs($decimals)); if ($value < 0) { $round = 0 - $round; } $value = PHPExcel_Calculation_MathTrig::MROUND($value, $round); } return PHPExcel_Style_NumberFormat::toFormattedString($value, $mask); }