[SOLVED] Number format (currency)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Foundation
Forum Newbie
Posts: 13
Joined: Mon Mar 24, 2003 9:06 pm

[SOLVED] Number format (currency)

Post 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!
Last edited by Foundation on Mon Apr 12, 2004 4:03 pm, edited 1 time in total.
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post 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;
}
?>
Foundation
Forum Newbie
Posts: 13
Joined: Mon Mar 24, 2003 9:06 pm

Post 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?
Last edited by Foundation on Mon Apr 12, 2004 3:39 pm, edited 1 time in total.
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post 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
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post 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>";
?>
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post 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
Foundation
Forum Newbie
Posts: 13
Joined: Mon Mar 24, 2003 9:06 pm

Post 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.
Post Reply