Forum Moderators: coopster

Message Too Old, No Replies

Eliminate negative signs on numbers

how to do in PHP

         

johnnydequino

12:34 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



For this script, the first two scenerios can return red, and it can look like this: -50

Anyway to strip out the - sign? It's stored in the database this way. (It's a number, decimal value 6,2)

<? if ($row->rebateprice < 0.00)
{$row->rebateprice = "($row->rebateprice)";
print "not just free, you make: <font color='#CC0000'>$row->rebateprice</font>"; }
elseif ($row->rebateprice == 0.00)
{$row->rebateprice = "($row->rebateprice)";
print "Free! Your cost: <font color='#CC0000'>$row->rebateprice</font>"; }
elseif ($row->rebateprice > 0.00)
{$row->rebateprice = "$row->rebateprice";
print "Your cost: <font color='#000000'>$row->rebateprice</font>"; }?>

jd

mincklerstraat

2:11 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try replacing $row->rebateprice in each of your print statements with str_replace('-', '', ($row->rebateprice)

johnnydequino

4:09 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



I tried replacing $row->rebateprice in the print area with str_replace('-', '', ($row->rebateprice), and it actually prints out:

str_replace('-', '', (-50.00) on the webpage.

Syntax issue maybe?

Thanks for all your help!

jd

jatar_k

4:18 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



multiply it by -1

jollymcfats

4:34 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Multiplication loses the formatting- these are (6,2) decimal numbers. -5.00 * -1 = 5

I use a little function like this to ensure that my currency amounts always print out with proper formatting:

function moneyAbs($amount) { 
return sprintf('%.2f', abs($amount));
}

In the orginal example, you might do this:


$row->rebateprice = "(" . moneyAbs($row->rebateprice) . ")";

johnnydequino

6:00 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



I used the abs function, it works, but I lost my decimals:

{$row->rebateprice = abs($row->rebateprice);

This = 50

I need 50.00

I tried the moneyabs but could not get it to return correctly. Anyway to keep the decimals?

Thanks all for your help!

jd

jatar_k

6:11 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



number_format(abs($amount),2);

johnnydequino

7:18 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Yes, number_format(abs($amount),2); did it. Thank you all!

jd

coopster

9:41 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For the record, the str_replace() [php.net] option suggested in msg#2 would have worked and yes, it was a syntax issue...an additional parenthesis that shouldn't have been there.
str_replace('-', '', ($row->rebateprice)
The main reason formatting in this fashion works is because values for columns of type DECIMAL are typically stored as string values (including the significant decimal place values) and will be returned as such.
Also, it isn't just multiplication that removes the formatting, but the conversion of the variable from string to number -- a var_dump() [php.net] statement will reveal that the variable is cast as string.

var_dump($row->rebateprice); // string(6) "-50.00"

Even a simple print() language construct will return the number without significant zeros when it is cast from string to number. And so will var_dump():

print (float) $row->rebateprice; // -50 
var_dump(abs($row->rebateprice)); // float(50)

Either way, I find that using a formatting technique such as number_format() or a user-defined function is a better practice when you want output formatted a certain way every time, no matter where the data is coming from or of what type it may have been cast.