Page 1 of 1

[SOLVED] Number format (currency)

Posted: Mon Apr 12, 2004 2:53 pm
by Foundation
Still kinda new to PHP. Here's my setup, question to follow.

Code: Select all

$result = ($value1 - $value2)

if($result > 0) { 
     echo "<span class='black'>\$$result</span>";
&#125; else &#123; 
     echo "<span class='red'>(\$$result)</span>";
The problem is that when the result is negative, it spits out a (-) minus sign in front of the value, as it logically should. However, for this project, it needs to designate negative values with parentheses and red text.

The output is ($-123.45) in red, but I need it to take the minus out and put it in as ($123.45) in red. I tried using absolute value but it causes the value to not trigger as negative obviously.

Sorry for the specific issue but I'm out of ideas with my very limited experience with PHP.

Thanks for any help!

Posted: Mon Apr 12, 2004 3:26 pm
by Ixplodestuff8
Chances are there is a built in function for that, but I can't think of any, and I couldn't find it by skimming the manual's section on math.

Anyways I made a simple function that checks if a number is less than 0, if it is, then it's multiplied by -1 (which turns it to positive), and if it isn't it just returns the original number.

Code: Select all

<?php
function take_out_the_negative ( $number )
{
    return ( $number < 0 ) ? $number * -1 : $number;
}
?>

Posted: Mon Apr 12, 2004 3:31 pm
by Foundation
That'll work for now. Good thinking!

If anyone finds a built in function or knows how to remove a generated character like that, please post.

Edit: Actually, after thinking about it for a sec, won't that negate the value and make it then think it's positive?

Posted: Mon Apr 12, 2004 3:36 pm
by Ixplodestuff8
I just wrote that to take out negatives anytime you'd need to, but to take out a character from a string you could use str_replace

str_replace ( "-", "", $string );

just replace the negative sign with anything and it'll remove it

Posted: Mon Apr 12, 2004 3:45 pm
by Ixplodestuff8
Foundation wrote:Edit: Actually, after thinking about it for a sec, won't that negate the value and make it then think it's positive?
Yes it will, you should run it at the time of displaying the number, after all checking to see if it's positive or negative is done, using your original code it would look like this:

Code: Select all

<?php
$result = ($value1 - $value2)

if($result > 0) {
     echo "<span class='black'>\$$result</span>";
} else {
     echo "<span class='red'>(\$" . take_out_the_negative ( $result ) . ")</span>";
?>

Posted: Mon Apr 12, 2004 3:52 pm
by andre_c
or do Ixplodestuff8's solution but using absolute value at the time of displaying it:

Code: Select all

echo "<span class='red'>(\$". abs($result) .")</span>";
just make sure you do it after checking for negative like Ixplodesuff8 said

Posted: Mon Apr 12, 2004 3:59 pm
by Ixplodestuff8
I knew there'd would have to be a function for that :P . The irony of course is that it's the first function in the list on the page I linked to. :o

Posted: Mon Apr 12, 2004 4:02 pm
by Foundation
Excellent! Ok thanks for the help. The ABS was what I thought could work but I didn't have my syntax right when displaying the result.

Thanks to both of you.