note 73069 modified in function.number-format by levim

From: Date: Sun, 23 Mar 2014 14:22:16 +0000
Subject: note 73069 modified in function.number-format by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199032@lists.php.net to get a copy of this message
I use the following to get around the negative zero problem: <?php function currency_format($amount, $precision = 2, $use_commas = true, $show_currency_symbol = false, $parentheses_for_negative_amounts = false) { /* ** An improvement to number_format. Mainly to get rid of the annoying behaviour of negative zero amounts. */ $amount = (float) $amount; // Get rid of negative zero $zero = round(0, $precision); if (round($amount, $precision) == $zero) { $amount = $zero; } if ($use_commas) { if ($parentheses_for_negative_amounts && ($amount < 0)) { $amount = '('.number_format(abs($amount), $precision).')'; } else { $amount = number_format($amount, $precision); } } else { if ($parentheses_for_negative_amounts && ($amount < 0)) { $amount = '('.round(abs($amount), $precision).')'; } else { $amount = round($amount, $precision); } } if ($show_currency_symbol) { $amount = '$'.$amount; // Change this to use the organization's country's symbol in the future } return $amount; } ?> --was-- I use the following to get around the negative zero problem: function currency_format($amount, $precision = 2, $use_commas = true, $show_currency_symbol = false, $parentheses_for_negative_amounts = false) { /* ** An improvement to number_format. Mainly to get rid of the annoying behaviour of negative zero amounts. */ $amount = (float) $amount; // Get rid of negative zero $zero = round(0, $precision); if (round($amount, $precision) == $zero) { $amount = $zero; } if ($use_commas) { if ($parentheses_for_negative_amounts && ($amount < 0)) { $amount = '('.number_format(abs($amount), $precision).')'; } else { $amount = number_format($amount, $precision); } } else { if ($parentheses_for_negative_amounts && ($amount < 0)) { $amount = '('.round(abs($amount), $precision).')'; } else { $amount = round($amount, $precision); } } if ($show_currency_symbol) { $amount = '$'.$amount; // Change this to use the organization's country's symbol in the future } return $amount; } http://php.net/manual/en/function.number-format.php

« previous php.notes (#199032) next »