Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 26th, 2008, 08:55 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
Default 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!

  #2  
Old March 26th, 2008, 09:05 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
Default 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.

  #3  
Old March 26th, 2008, 10:25 PM
Wiktor Walc
Guest
 
Posts: n/a
Default 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
  #4  
Old March 26th, 2008, 10:45 PM
Alexey Kulentsov
Guest
 
Posts: n/a
Default 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);




  #5  
Old March 26th, 2008, 11:35 PM
Michael Fesser
Guest
 
Posts: n/a
Default 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
  #6  
Old March 27th, 2008, 01:35 AM
Alexey Kulentsov
Guest
 
Posts: n/a
Default 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));
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles