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! | | | | 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. | | | | 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 | | | | 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); | | | | 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 | | | | 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)); |  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
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 226,582 network members.
|