Jump to content

[SOLVED] Manipulating integers


shamuraq

Recommended Posts

Hi All,

 

I would like to add a positive sign "+" infront of a positive integer. Is there a proper syntax to it without the <echo> function so that php still treat it as integer rather than a variable for further mathematical function to take place?

 

-thanx all

Link to comment
https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/
Share on other sites

Hi All,

 

I would like to add a positive sign "+" infront of a positive integer. Is there a proper syntax to it without the <echo> function so that php still treat it as integer rather than a variable for further mathematical function to take place?

 

-thanx all

 

do a function

 

function format($number){

    if($number > 0) return "+".$number;

    else if($number < 0) return $number;

    else if($number == 0) return $number;

}

 

 

echo format(1);

Just adding a "+" to it converts it to a string.

 

Also you can just use sprintf instead of nadeemshafi9's format().

 

$a = "2";

echo sprintf("%+d\n", $a);
echo sprintf("%+d\n", -$a);

 

i done C ADA C++ JAVA still to this day i cant get my head around %d malarky

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.