Is there a PHP function to format currency amount in accounting format?

I am currently writing a PHP report that sometimes needs to display negative currency amounts. The indicated currency amounts are stored in a database of type "-44.00". Ideally, this number would appear as "($ 44.00)" on the report.

I know I can write some semi-arid function to determine if a number is negative and insert parentheses manually, but I was wondering if there is some convenient PHP function that can do this for me before I reinvent the wheel. I searched around and didn't find anything similar to this exact problem. I know about money_format, but I don't see any way to do the negative / parenthesis. Keep in mind that the code must function, whether the number is negative or positive.

+2


source to share


3 answers


http://www.php.net/manual/en/function.money-format.php



echo money_format('%(n', '-44.00');

      

+7


source


function format_currency($amount) {
    if($amount < 0)
        return "($".$amount.")";

    else return "$".$amount;
}

      



+1


source


Hmm, sort of ... but it will still give out a minus sign when it is negative. I would change this function to something like:

function accting_format($amount) {
    if ($amount < 0) return '($' . abs($amount) . ')';
    return '$' . $amount;
}

      

Note that abs () have parentheses copied around the sum.

0


source







All Articles