tags:

views:

122

answers:

5

Hay all, is there a PHP function which adds "+" to positive strings?

i.e

function(4) // returns +4
function(1.0) // returns +1.0
function(-1) // returns -1
function(0) // returns +0

Thanks

EDIT: wants to take and return strings.

A: 

Home made function

function make_positive($int){
    if($int >= 0){
        return "+".$int;
    }else{
        return $int;
    }
}

Does PHP have its own?

EDIT: changed function name.

dotty
The only problem I see is that if its positive, your function returns a string, if its negative, it returns the integer. It probably should return a consistent type, right?
Doug Neiner
Also, a function named `is_foo` suggests it should return a `boolean` value.
nikc
Edited, thanks nikc.
dotty
make_positive is also a bad name, since that would imply it's returning the absolute value. How about "add_sign"
therefromhere
+3  A: 

You could use (s)printf with the following:

$number = sprintf('%+f', $number);
// "-0"  => +0.000000
// "1.2" => +1.200000

or

function formatPositive($number)
{
    return ($number > 0) ? "+$number" : $number;
    // "0"   => "0"
    // "-0"  => "-0"
    // "1.2" => "+1.2"
}

or

function formatPositive($number)
{
    switch(true) {
        case !is_numeric($number): // "Beer" => "NaN"
            $number = 'NaN'; 
            break;
        case $number == 0: // "-0" = "±0"
            $number "±0";
            break;
        case $number > 0: // "1.23" => "+1.23" 
            $number = "+$number";
            break;
        default:  // "-1.23" => "-1.23" 
            break;
    }
    return "$number";
}
Gordon
Close, but it should be `>=` since he wants it to say `+0`. Also, shouldn't the second part of the ternary operator be `"$number"` so the function consistently returns a `String` vs. a string if positive and an integer if negative? (Oh, and +1 for using ternary form. Looks great)
Doug Neiner
sending "nothing" to that returns "+"
dotty
@Doug Zero is not a positive number, so it should not return + imho. If any it should have ±. Since PHP will automatically typecast as needed, I leave it up to the OP to decide on the return type.
Gordon
@dotty: because you passed a string.
Gordon
Marked as correct after checking against my code.
dotty
If $number is -0 (as returned by, e.g., _ceil(-.7)_ or _round(-.2))_, the first solution returns "+0.000000", the second returns "-0", and the third returns "±-0".
GZipp
@GZipp pick the result you like most ;)
Gordon
I don't like any of them. I was just pointing out some anomolies so that anyone else using your code would know what to expect.
GZipp
@Gzipp i didnt notice the plusminusminus in the third one. fixed that. thanks
Gordon
+3  A: 
sprintf("%+d", 5);    # should give +5
sprintf("%+d", -5);   # should give -5

Quick reference: http://www.php.net/sprintf

Chris Jester-Young
+8  A: 
$i = 3;
printf('%+d ', $i);

$i = -3;
printf('%+d ', $i);

prints +3 -3 (and also works with sprintf() if needed)

VolkerK
You'd need a distinct line if the value is a float though.
Tor Valamo
A: 

The PHP language reference for sprintf suggests "%+d" as a format specifier for positive signed integers.

pavium