note 88486 modified in function.number-format by felipe

From: Date: Mon, 02 Feb 2009 20:59:03 +0000
Subject: note 88486 modified in function.number-format by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150129@lists.php.net to get a copy of this message
I was looking for a SIMPLE way to format currency and account for negative values while not losing the calculation properties of my number. Here's my function - it's not rocket science, but maybe can help someone along the way. <?php function wims_currency($number) { if ($number < 0) { $print_number = "($ " . str_replace('-', '', number_format ($number, 2, ".", ",")) . ")"; } else { $print_number = "$ " . number_format ($number, 2, ".", ",") ; } return $print_number; } ?> Sample use: <?php $pur_po_total = ($pur_po_total + $pur_item_total); $print_pur_po_total = wims_currency($pur_po_total); ?> Returns (for example) $ 44,561.00 or, if a negative ($ 407,250.00) This way, I use my 1st variable for calculations and my 2nd variable for output. I'm sure there are better ways to do it, but this got me back on track. --was-- I was looking for a SIMPLE way to format currency and account for negative values while not losing the calculation properties of my number. Here's my function - it's not rocket science, but maybe can help someone along the way. function wims_currency($number) { if ($number < 0) { $print_number = "($ " . str_replace('-', '', number_format ($number, 2, ".", ",")) . ")"; } else { $print_number = "$ " . number_format ($number, 2, ".", ",") ; } return $print_number; } Sample use: $pur_po_total = ($pur_po_total + $pur_item_total) ; $print_pur_po_total = wims_currency($pur_po_total); Returns (for example) $ 44,561.00 or, if a negative ($ 407,250.00) This way, I use my 1st variable for calculations and my 2nd variable for output. I'm sure there are better ways to do it, but this got me back on track. http://php.net/manual/en/function.number-format.php

« previous php.notes (#150129) next »