sprintf and negative number sign?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawpoop@gmail.com

    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!

  • lawpoop@gmail.com

    #2
    Re: sprintf and negative number sign?

    On Mar 26, 1:53 pm, lawp...@gmail.c om wrote:
    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.

    Comment

    • Wiktor Walc

      #3
      Re: sprintf and negative number sign?

      lawpoop@gmail.c om pisze:
      On Mar 26, 1:53 pm, lawp...@gmail.c om wrote:
      >
      >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.2 f", 3.45));

      --
      CKFinder :: ajax web file browser

      Comment

      • Alexey Kulentsov

        #4
        Re: sprintf and negative number sign?

        lawpoop@gmail.c om wrote:
        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);




        Comment

        • Michael Fesser

          #5
          Re: sprintf and negative number sign?

          ..oO(Alexey Kulentsov)
          >lawpoop@gmail. com wrote:
          >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

          Comment

          • Alexey Kulentsov

            #6
            Re: sprintf and negative number sign?

            Michael Fesser wrote:
            >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));

            Comment

            Working...