Connecting Tech Pros Worldwide Help | Site Map

sprintf and negative number sign?

  #1  
Old March 26th, 2008, 08:55 PM
lawpoop@gmail.com
Guest
 
Posts: n/a
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

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

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

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

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sprintf krister@kalleanka.se answers 15 June 15th, 2007 09:25 PM
sprintf >> atof question tjay answers 5 July 23rd, 2006 03:15 PM
C function for returning number of digits? Luke Wu answers 27 November 30th, 2005 07:15 PM
IBM 32-bit Floating Point Conversion magoo answers 0 July 19th, 2005 06:04 AM