Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    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

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Maybe with a "sign specifier"?
    http://php.net/manual/en/function.sprintf.php

    Or it might be just easier to do it manually?

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    I almost got it,

    PHP Code:
    $dada $num1 += (-$num2); 
    Sonny

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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

  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •