shamuraq Posted April 15, 2009 Share Posted April 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/ Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 No, there isn't. Use integers for calculations. Convert to string when displaying. Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810554 Share on other sites More sharing options...
nadeemshafi9 Posted April 15, 2009 Share Posted April 15, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810556 Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 But it's not an integer anymore Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810558 Share on other sites More sharing options...
shamuraq Posted April 15, 2009 Author Share Posted April 15, 2009 No, there isn't. Use integers for calculations. Convert to string when displaying. So how do i convert it to string? Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810562 Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810575 Share on other sites More sharing options...
shamuraq Posted April 15, 2009 Author Share Posted April 15, 2009 thanx Mchl. I truly appreciate a;; the help. Thanx y'all Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-810692 Share on other sites More sharing options...
nadeemshafi9 Posted April 21, 2009 Share Posted April 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-815605 Share on other sites More sharing options...
Daniel0 Posted April 21, 2009 Share Posted April 21, 2009 %d will just be replaced by a signed integer. That should be a fairly simple concept and it's consistent over all languages that has a printf-ish function. Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-815621 Share on other sites More sharing options...
shamuraq Posted April 22, 2009 Author Share Posted April 22, 2009 Thanx again Daniel0 Quote Link to comment https://forums.phpfreaks.com/topic/154186-solved-manipulating-integers/#findComment-816056 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.