Connecting Tech Pros Worldwide Forums | Help | Site Map

sprintf and negative number sign?

lawpoop@gmail.com
Guest
 
Posts: n/a
#1: Mar 26 '08
Hello all -

I have sprintf formatting a number that goes on a report. I'm using
%05.2f to change 5.5 to 05.50, for example. However, I'm having a
problem with negative values -- I get -3.43, for example, because with
the sign, the whole length of the string is 5 characters.

I want a number that always has the tens place and the hundredths
decimal place, and the sign only when the number is negative. For
example:
05.50
-01.22
55.07
-80.00
00.40

How do I get what I want? Obviously, I don't really understand the
syntax of sprintf. Thanks!


lawpoop@gmail.com
Guest
 
Posts: n/a
#2: Mar 26 '08

re: sprintf and negative number sign?


On Mar 26, 1:53 pm, lawp...@gmail.com wrote:
Quote:
I have sprintf formatting a number that goes on a report. I'm using
%05.2f to change 5.5 to 05.50, for example. However, I'm having a
problem with negative values -- I get -3.43, for example, because with
the sign, the whole length of the string is 5 characters.
Also note, I tried simply using %06.2f, and while that does give me
-03.33, it will give me 003.32 when the number is not negative. So,
any help is appreciated.

Wiktor Walc
Guest
 
Posts: n/a
#3: Mar 26 '08

re: sprintf and negative number sign?


lawpoop@gmail.com pisze:
Quote:
On Mar 26, 1:53 pm, lawp...@gmail.com wrote:
>
Quote:
>I have sprintf formatting a number that goes on a report. I'm using
>%05.2f to change 5.5 to 05.50, for example. However, I'm having a
>problem with negative values -- I get -3.43, for example, because with
>the sign, the whole length of the string is 5 characters.
>
Also note, I tried simply using %06.2f, and while that does give me
-03.33, it will give me 003.32 when the number is not negative. So,
any help is appreciated.
>
You can't achieve that with a single sprintf call, at least I don't
think it's possible.

Perhaps add a "+" to force the sign at the beginning and then delete it.
str_replace("+", "", sprintf("%+06.2f", 3.45));

--
CKFinder :: ajax web file browser
http://www.ckfinder.com
Alexey Kulentsov
Guest
 
Posts: n/a
#4: Mar 26 '08

re: sprintf and negative number sign?


lawpoop@gmail.com wrote:
Quote:
Hello all -
>
I have sprintf formatting a number that goes on a report. I'm using
%05.2f to change 5.5 to 05.50, for example. However, I'm having a
problem with negative values -- I get -3.43, for example, because with
the sign, the whole length of the string is 5 characters.
>
I want a number that always has the tens place and the hundredths
decimal place, and the sign only when the number is negative. For
example:
05.50
-01.22
55.07
-80.00
00.40
>
How do I get what I want? Obviously, I don't really understand the
syntax of sprintf. Thanks!
sprintf("%c%05.2f", $value < 0 ? '-' : ' ',$value);




Michael Fesser
Guest
 
Posts: n/a
#5: Mar 26 '08

re: sprintf and negative number sign?


..oO(Alexey Kulentsov)
Quote:
>lawpoop@gmail.com wrote:
Quote:
>Hello all -
>>
>I have sprintf formatting a number that goes on a report. I'm using
>%05.2f to change 5.5 to 05.50, for example. However, I'm having a
>problem with negative values -- I get -3.43, for example, because with
>the sign, the whole length of the string is 5 characters.
>>
>I want a number that always has the tens place and the hundredths
>decimal place, and the sign only when the number is negative. For
>example:
>05.50
>-01.22
>55.07
>-80.00
>00.40
>>
>How do I get what I want? Obviously, I don't really understand the
>syntax of sprintf. Thanks!
>
>sprintf("%c%05.2f", $value < 0 ? '-' : ' ',$value);
sprintf('%s%05.2f', $value < 0 ? '-' : '', abs($value));

Micha
Alexey Kulentsov
Guest
 
Posts: n/a
#6: Mar 27 '08

re: sprintf and negative number sign?


Michael Fesser wrote:
Quote:
Quote:
>sprintf("%c%05.2f", $value < 0 ? '-' : ' ',$value);
sprintf('%s%05.2f', $value < 0 ? '-' : '', abs($value));
Yes, thanks for fixing it. But space still need here so right variant is:
sprintf("%s%05.2f", $value < 0 ? '-' : ' ',abs($value));
Closed Thread