Announcement

Collapse
No announcement yet.

Format$ Weirdness

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Format$ Weirdness

    Format$ doesn't seem to format fractional negative values right. In my case I'm
    tying to format a number like "-.23" so that it comes out "-0.23000". So I use

    Format$(-.23, "0.00000")

    But the output is actually "-.23000", contrary to what the docs say about adding
    leading zero to the format string.

    However, this does work for positive numbers:

    Format$(.23, "0.00000")

    will output "0.23000"

    I can work around this by using "*0.00000" for the formatting string, but I'd
    like to know what's going on. Here's a short sample program:

    Code:
    $COMPILE EXE
    $REGISTER NONE
    
    FUNCTION PBMAIN() AS LONG
       
       DIM sTmp AS STRING
       
       ' This should display "-0.23000", but it doesn't
       sTmp = FORMAT$(-.23,"0.00000")
       
       MSGBOX sTmp, 0, "Should show '-0.23000'"
       
       
       ' Workaround
       sTmp = FORMAT$(-.23,"*0.00000")
    
       MSGBOX sTmp, 0, "Should show '-0.23000'"
       
       ' This should display "+0.23000", and it does
       sTmp = FORMAT$(.23,"+0.00000")
    
       MSGBOX sTmp, 0, "Should show '+0.23000'"
    
    END FUNCTION
    ------------------
    Mark Newman

    [This message has been edited by Mark Newman (edited August 22, 2000).]
    Mark Newman

  • #2
    Mark,

    To achieve your goal you have to use the following format

    FORMAT$(-.23,"00.00000")

    This format allows for two characters in front of the dot
    whilst yours makes a provision for only one character.
    Hence, in the case of a negative number only minus is shown.

    Edmund

    ------------------

    Comment


    • #3
      Thanks Edmund!

      This isn't very clear from the docs, so perhaps a few more
      examples in the manual would help.



      ------------------
      Mark Newman
      Mark Newman

      Comment


      • #4
        As I understand it from discussions with the Documentation staff, the FORMAT$() section in the help/doc's is going to be completely revised in a future update to the compiler. Essentially, the "0" digit is also a placeholder for the negation symbol, so two leading zero's (or pound "#" signs are required to ensuree that at least one leading zero retained for negative values.

        However, it creates a problem if you use zero's for digit placeholders as positive values will have leading zeros, which may not be what you want. The solution is to use pound symbols instead of zero digits, for example:
        Code:
         sTmp = FORMAT$(1.23,"00.00000") ' "01.23000"
         sTmp = FORMAT$(1.23,"#0.00000") ' "01.23000"
         sTmp = FORMAT$(1.23,"##.#####") ' " 1.23000" <-- preferred
         
         sTmp = FORMAT$(-1.23,"00.00000") ' "-1.23000"
         sTmp = FORMAT$(-1.23,"#0.00000") ' "-1.23000"
         sTmp = FORMAT$(-1.23,"##.#####") ' "-1.23000" <-- preferred
        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          sTmp = FORMAT$(-1.23,"##.#####") ' "-1.23000" <-- preferred
          Using the "#" formatter in this way runs counter to the docs, which state
          that the "#" will output a space if there aren't enough digits to fill the
          format string on either side of the decimal.

          One suggestion: Many (many!) years ago I used HP's RMB (Rocky Mountain Basic)
          which had a sign placement formatter (I think it was the "+" sign), so that
          a format string such as "+0.000" would always output the sign of the expression.
          A handy feature; how about adding this to the wish list?

          Thanks!

          ------------------
          Mark Newman

          [This message has been edited by Mark Newman (edited August 23, 2000).]
          Mark Newman

          Comment


          • #6
            I'll ask it to be put on the suggestions list. Thanks!


            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment

            Working...
            X