Announcement

Collapse
No announcement yet.

Strange result from using$ with format string "#.###"

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

  • Strange result from using$ with format string "#.###"

    This seems like a bug in the using$ function. When the number being formatted is close to -1 (say -.9999), and the format string (i.e. "#.##") has less decimal places the the number, instead of -1.00 as the result it becomes -.00

    for example
    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
    
        PRINT USING$("#.##", 22.999)
        PRINT USING$("#.##", 2.999)
        PRINT USING$("#.##", 1.999)
        PRINT USING$("#.##", .999)
        PRINT USING$("#.##", -.999)
        PRINT USING$("#.##", -.997)
        PRINT USING$("#.##", -.992)
        PRINT USING$("#.##", -1.999)
        PRINT USING$("#.##", -2.999)
    
        WAITKEY$
    
    
    END FUNCTION
    i would expect the results to be:
    23.00
    3.00
    2.00
    1.00
    -1.00
    -1.00
    -.99
    -2.00
    -3.00

    but the results are:
    23.00
    3.00
    2.00
    1.00
    -.00
    -.00
    -.99
    -2.00
    -3.00

    I had some acceleration data where i was reading the data and writing it to a file with a format "#.###", and i kept getting zero values where i knew the actual value was near -1.0.

    An easy fix is to use the format string ##.###, but it took me about an hour to figure out the source of the error.

    Comments?


  • #2
    Use ROUND, e.g.

    USING$("#.##", ROUND(-.997, 2))
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Looks strange, yes. Else PRINT FORMAT$(-.997, "0.00") gives expected result.

      Comment


      • #4
        Or use "##.##", to make room for the - sign.

        PRINT USING$("##.##", -.999) will print -1.
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          Well, the docs for USING$ do say
          If the number is negative, a minus sign occupies a digit position.
          so it seems if you're expecting negatives you need to accommodate the minus sign.
          Rod
          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

          Comment


          • #6
            Originally posted by Rodney Hicks View Post
            Well, the docs for USING$ do say
            If the number is negative, a minus sign occupies a digit position.
            so it seems if you're expecting negatives you need to accommodate the minus sign.
            IOW, performing as documented. No Bug

            Comment


            • #7
              When working with whole cents this gives the same results with all data types and handles 0 the same.
              Code:
              FUNCTION PBMAIN () AS LONG
                LOCAL ints AS INTEGER,quads AS QUAD, exts AS EXT,cuxs AS CUX
                LOCAL sResults AS STRING
                FOR ints = -100 TO 100 STEP 10
                  quads = ints
                  exts = ints
                  cuxs = ints
                  sResults+= USING$("&  &  &  &" + $CR,money(ints),money(quads),money(exts),money(cuxs))
                  NEXT
                  ? sResults,,"Integer,Quad, Extended, CUX"
              END FUNCTION
              
              FUNCTION Money(BYVAL Pennies AS EXT) AS STRING
                FUNCTION = SWITCH$(  _
                 pennies = 0, ".00", _
                 pennies > -100 AND pennies < 100, USING$(".##",pennies * .01##), _
                 pennies< > 0,TRIM$(USING$("#,.##",pennies * .01##)))
              END FUNCTION
              How long is an idea?

              Comment


              • #8
                Because of the differences in the formatting capabilities between USING$() and FORMAT$() I sometimes use both in the same statement with the return from FORMAT$() as a pure "string" argument for USING$() ...

                Code:
                     PrintLine = USING$  ("  invoice _# #######     Amount &",  inv_no&, FORMAT$(ROUND (invc_amt,2))
                That's probably a bad example but you get the idea. You can have the literals and mulitple variables "power" of USING$()) and if you cannot get that to format as desired to you can just call it a string and use FORMAT$() or any other function you have to get the formatting you want. .. eg one of these formatting MACROs I use to return a string

                Code:
                MACRO fmt_Int(intval)    = RSET$(FORMAT$(intval,"* #,"),7)
                MACRO fmt_Bigint (X)     = RSET$(FORMAT$(X,"#,"),14)
                MACRO fmt_money (curval) = RSET$(FORMAT$(curval, "$#,.00"),12)
                MACRO fmt_money2 (curval)= RSET$(FORMAT$(curval, "$#,.00 ;$#,.00-"),12) ' gives '0' in dollar position
                MACRO fmt_money3 (curval)= RSET$(FORMAT$(curval, "$#,.00 ;$#,.00-; "),12) ' gives 'blank when zero'
                
                MACRO fmt_Int0(intval, fsize)    = RSET$(LTRIM$(STR$(intval)), fsize USING "0")
                Eg my sample code could read..
                Code:
                PrintLine = USING$  ("  invoice _# #######     Amount &",  inv_no&, fmt_money ( invc_amt))
                .. which handles the right-justification of the dollar amount



                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  This demonstrates the difference in handling 0 amounts.
                  Code:
                  FUNCTION PBMAIN () AS LONG
                    ? PrintLine(2,0),,"Zero invoice"
                  END FUNCTION
                  
                  FUNCTION Money(BYVAL Pennies AS EXT) AS STRING
                    FUNCTION = SWITCH$(  _
                     pennies = 0, ".00", _
                     pennies > -100 AND pennies < 100, USING$(".##",pennies * .01##), _
                     pennies< > 0,TRIM$(USING$("#,.##",pennies * .01##)))
                  END FUNCTION
                  
                  FUNCTION PrintLine(inv_No AS LONG,invc_amt AS EXT) AS STRING
                    LOCAL s AS STRING
                    s = USING$  ("  invoice _# #######     Amount &",  inv_no&, FORMAT$(ROUND (invc_amt,2)*.01##))
                    FUNCTION = s + $CR + USING$("  invoice _# #######     Amount &",  inv_no&, Money(invc_amt))
                  END FUNCTION
                  How long is an idea?

                  Comment

                  Working...
                  X