
March 26th, 2008, 08:55 PM
| | | sprintf and negative number sign?
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! | 
March 26th, 2008, 09:05 PM
| | | 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. | 
March 26th, 2008, 10:25 PM
| | | 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 | 
March 26th, 2008, 10:45 PM
| | | 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); | 
March 26th, 2008, 11:35 PM
| | | 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 | 
March 27th, 2008, 01:35 AM
| | | 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)); |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|