Forum Moderators: coopster

Message Too Old, No Replies

number format brackets instead of -ve

         

jackvull

1:32 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



I need to display negative numbers as (1,950) instead of -1950.
Anyway to do that?

number_format($varTotal);

Also, numbers that are 0 I need to replace with a dash -
can't use str_replace as it will replace all zeros with a dash won't it?

jackvull

1:42 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



on windows so money_format won't work

hughie

2:00 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



can't you just multiply by -1

-1950 * -1 = 1950

coopster

3:33 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could get the absolute value of the number by using abs() [php.net] if the number was less than zero. I would probably write my own user-defined function to process the numbers.

ChainsawXIV

3:59 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



You could do something complicated with regular expressions, but probably your strongest play here is to take advantage of the fact that PHP deals with variable type implicitly.

Your integers are strings when you use them that way, so you can do a simple if, to screen for a negative value, and then immediately treat the value as a string, cropping off the - character, and concatenating your parenthesis:

if($value < 0){$value = '(' . substr($value,1) . ')';}

Similarly, you can simply look for the entire money value to be equal to zero, and replace it with the - character if it is. No need to fret about the other digits or characters, since comparing to the numeric 0 will test correctly:

if($value == 0){$value = '-';}

In principle, you can wrap this all up into your own function, to make things easy if you're going to do this in a bunch of places:

function FormatCash($value){
if($value < 0){$value = '(' . substr($value,1) . ')';}
elseif($value == 0){$value = '-';}
return $value;
}

[edited by: ChainsawXIV at 4:06 pm (utc) on Jan. 23, 2008]

jackvull

2:47 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



apparently the css will work in excel:

mso-number-format:\#\,\#\#0\.000

but I need to get this format:
#,##0_);(#,##0);""-""

how do you escape these i CSS?