Forum Moderators: coopster

Message Too Old, No Replies

sprintf or else to force the sign of a integer

How to get php to echo + for positive integers?

         

gg1983

5:42 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Hi all!

I'm fetching a variable value from a database. It's a timezone so it's something between -12 and 12 (including 0), like:

$time_zone=$row['time_zone'];

If for example,

time_zone=2
in the database,I want the script to echo "timezone:UTC+2". Is there a better way than the if loop, something like sprintf to do this?
I have been trying for hours with the help of [php.net...] and this and other fora but I couldn't do anything.
I've tried with (all?) sprintf and multiple args, so its something like:

sprintf('my string with args %1$s and %3$s and timezone=%2\$+u', $arg1, "2", $arg3);

or similar, and I'm getting output like "my string with args firstArg and thirdArg and timezone=$+d".

I'm desperate! Any help? Is the backslash needed? How does the formatting go? Thanks !
Btw if you work for php.net, please tell them to update the formatting documentation for multiple arguments, I smell the problem is somewhere there...

eelixduppy

5:54 am on Aug 20, 2008 (gmt 0)



Hello.

I think you want the string in the sprintf arg to be like this:


timezone:%2$d+

gg1983

7:28 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Thanks for this fast reply!
Unfortunately, the output of this is not what expected.

arg=1 (positive), the output is 1+ (instead of +1)
arg=-7 (negative), the output is -7+ ...

Looks like there is always an extra trailing "+" but no preceeding sign (even for positive number)...
Should I go for

if ($timezone>0) { $timezone = "+" . $timezone; }
?

Thanks for your time.

seabird505

8:06 am on Aug 20, 2008 (gmt 0)

10+ Year Member



A compact way to display signed numbers is like

$timezone = $row['time_zone'];
echo sprintf("timezone:UTC%s%d", ($timezone==abs($timezone)? "+" : "-"), abs($timezone));

or
echo sprintf("timezone:UTC%+d", $timezone);

gg1983

9:15 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Thanks seabird !
I knew I would end up using this kind of code that does work but is not very elegant...

I will mark this thread as solved (I don't even know if this is possible..!) as soon as someone explains me with a working example what the documentation (see link in post #1) means here:

Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:

1. An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well, and was added in PHP 4.3.0.

I use php v5+...

eelixduppy

1:28 pm on Aug 20, 2008 (gmt 0)



seabird has shown you above.

echo sprintf("timezone:UTC%+d", $timezone);

Should be what you are looking for and what the documentation was talking about.

gg1983

3:34 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



eelixduppy,

I currently use seabird's solution.
As I said in post #3, your solution in post #2 did not work (see above for output). To be honest I did not really understand what documentation says about "placeholders", but I really need them as the function

sprintf($format, $param)
is in the script (and so is
$param
) but
$format
is included with a language file...

So if the

$timezone
is parameter #6 and the formatting is built accordingly with the doc, I should have:
1-
%6
-> the param to use (here $timezone="7" or $timezone=7, does it make a difference?)
2-
%6+
-> "optional sign specifier" to force the sign to be displayed
3-
%6+d
-> to be "presented as a (signed) decimal number"

The formatting string (that matches the one without placeholder you posted in the above message) is built according to the elements' order and outputs:

$timezone="7" // +7 correct

$timezone="-7" // +-7 wrong
...

As I said seabird's code works perfectly (OK, I could tweak it to hide "UTC+0" ("UTC" is good) but I guess

sprintf()
will no do that... I'm just following-up for educational purposes!

Cheers...