Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2012, 01:22 AM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
doing math displaying + or -

Hi

I am trying to display a point differential result from two db fields

example
10-minus 9, and display +1
Is that possible? or do I need to add the + sign to the result manually

Thanks
Sonny
sonny is offline   Reply With Quote
Old 05-05-2012, 03:31 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Maybe with a "sign specifier"?
http://php.net/manual/en/function.sprintf.php

Or it might be just easier to do it manually?
mlseim is offline   Reply With Quote
Old 05-05-2012, 03:46 AM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
I almost got it,

PHP Code:
$dada $num1 += (-$num2); 
Sonny
sonny is offline   Reply With Quote
Old 05-05-2012, 06:48 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Wut? This simply assigns $dada the new value of $num1 which is equal to the negative value of $num2 added to $num1. This still is a number and will not show the sign unless its negative. This will also adjust the value of $num1.
Did you read the link provided? Use a print formatter to keep it as a number and add a sign:
PHP Code:
$var 15;
printf('%+d'$var); 
Will show +15.
Fou-Lu is offline   Reply With Quote
Old 05-06-2012, 12:26 AM   PM User | #5
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
Wut? This simply assigns $dada the new value of $num1 which is equal to the negative value of $num2 added to $num1. This still is a number and will not show the sign unless its negative. This will also adjust the value of $num1.
Did you read the link provided? Use a print formatter to keep it as a number and add a sign:
PHP Code:
$var 15;
printf('%+d'$var); 
Will show +15.
Ok I got it, the problem I had was I needed to put that into a variable.
It seems to work, but if anyone sees something wrong anyway
please let me know.

PHP Code:
$var=10;
$num=@sprintf('%+d'$var);

//Example
echo "<td>" $num "</td>; 

Last edited by sonny; 05-06-2012 at 12:42 AM..
sonny is offline   Reply With Quote
Old 05-06-2012, 11:50 AM   PM User | #6
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
why not this?
PHP Code:
$var=10;
echo 
"<td>" printf('%+d'$var) . "</td>; 
just use a print formatter at the point where you output the value

adding the extra line in there seems redundant
jmj001 is offline   Reply With Quote
Old 05-06-2012, 03:54 PM   PM User | #7
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by jmj001 View Post
why not this?
PHP Code:
$var=10;
echo 
"<td>" printf('%+d'$var) . "</td>; 
just use a print formatter at the point where you output the value

adding the extra line in there seems redundant
Your right, I used something like that before, don't know why I did not
this time. their must have been a reason, just forgot it.

Sonny
sonny is offline   Reply With Quote
Old 05-06-2012, 06:05 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
If you are straight out printing it, just use a printf:
PHP Code:
printf('<td>%+d</td>'$var); 
Much faster.
You may have wanted to store it as a string for later uses. Not really necessary since printf is a construct so you'll see almost no performance loss by using it.

Edit:
Actually, printf is not considered a construct; how odd. Still, I can't see tremendous overhead in its use.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:32 AM.


Advertisement
Log in to turn off these ads.