Announcement

Collapse
No announcement yet.

Format$ problem...

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

  • Format$ problem...

    I'm trying to use Format$ to present a value, but cannot remember how to fix all cases in one take - or if it's even possible - without using IIF$ work-around. Problem is this - if value is 0, I want to show "0" and if value is for example 0.75 I want to show "0.75". But, while using FORMAT$(0) gives correct "0", same FORMAT$(0.75) without mask gives ".75" without leading 0. So I add a mask, FORMAT$(0.75, "0.##") and get correct "0.75", but then FORMAT$(0, "0.##") result in "0." - with an additional decimal dot that I don't want.

    The only fix for this I can come up with is using IIF$, like IIF$(0, FORMAT$(0, "0.##"), FORMAT$(0)) where result actually is sort of "wrong" alternative when value is 0 (is then uses second alternative, which gives correct result), but expected correct when there is fractional part (like 0.75), but it surely it should be a better way? I just cannot figure out correct mask to solve this using FORMAT$ alone. Is it even possible? Clear as mud, I know - code below shows problem and results better.

    '
    Code:
    FUNCTION PBMAIN () AS LONG
    
      MSGBOX FORMAT$(0.75, "#.##") + "  " + FORMAT$(0.75) + $CRLF + _
             FORMAT$(0, "0.##")    + "  " + FORMAT$(0) + $CRLF + _
             $CRLF + _
             IIF$(0.75, FORMAT$(0.75, "0.##"), FORMAT$(0.75)) + "  " + _
             IIF$(0, FORMAT$(0, "0.##"), FORMAT$(0))
    
    '  Result - copied from MsgBox
    '  ---------------------------
    '  PowerBASIC
    '  ---------------------------
    '  0.75  .75  with mask, ok
    '  0.  0      with mask, not ok
    
    '  0.75  0    IIF$, all ok
    
    END FUNCTION
    '

  • #2
    Ok, can forget it - problem solved by using 3 masks, like:
    '
    Code:
    FUNCTION PBMAIN () AS LONG
    
      MSGBOX FORMAT$(0.75, "0.##;0.##;0") + "  " + FORMAT$(0, "0.##;0.##;0")
    
    '  Result - copied from MsgBox
    '---------------------------
    'PowerBASIC
    '---------------------------
    '0.75  0
    
    END FUNCTION
    '

    Comment


    • #3
      Originally posted by Borje Hagsten View Post
      Ok, can forget it - problem solved by using 3 masks, like:
      '
      Code:
      FUNCTION PBMAIN () AS LONG
      
      MSGBOX FORMAT$(0.75, "0.##;0.##;0") + " " + FORMAT$(0, "0.##;0.##;0")
      
      ' Result - copied from MsgBox
      '---------------------------
      'PowerBASIC
      '---------------------------
      '0.75 0
      
      END FUNCTION
      '
      Just be aware that your second mask will suppress the "-" sign for a negative number

      Comment


      • #4
        Ah yes, very good point, Stuart - thanks! But I also don't want a + sign for positive value, som correct mask should be:
        '
        Code:
        FUNCTION PBMAIN () AS LONG
        
          MSGBOX FORMAT$(0.75, "0.##;-0.##;0") + "  " + FORMAT$(0, "0.##;-0.##;0")
        
        END FUNCTION
        '

        Comment


        • #5
          Just refreshing my memory by re-reading Help on Format$ and I was reminded of why I dislike type specifier suffixes.

          I kept on reading the example
          x$ = FORMAT$(1 / 5!, "0.0%")
          as
          x$ = FORMAT$(1 / Factorial(5), "0.0%")

          Comment


          • #6
            Yes, I think the same, Stuart. Final solution, since I will be using this in several places - create and use a $mask equate for up to max number of decimals (18) and avoid having to type all those ###... for each Format$:
            '
            Code:
            $Mask = "0.##################;-0.##################;0"
            
            FUNCTION PBMAIN () AS LONG
            
              MSGBOX FORMAT$(0.75, $Mask) + " " + FORMAT$(-0.75, $Mask) + " " + FORMAT$(0, $Mask)
            
            END FUNCTION
            '

            Comment


            • #7
              Rounding?

              Click image for larger version

Name:	junk.png
Views:	100
Size:	99.4 KB
ID:	797411

              Code:
              #DIM ALL
              $Mask = "0.##################;-0.##################;0"
              FUNCTION PBMAIN () AS LONG
               MSGBOX FORMAT$(0.75!, $Mask) + " " + FORMAT$(-0.75!, $Mask) + " " + FORMAT$(0!, $Mask)
               DIM sArray(1 TO 999) AS STRING
               LOCAL Counter     AS LONG
               LOCAL Amount      AS SINGLE
               FOR Amount = -.090! TO .090 STEP .01!
                INCR counter
                sArray(counter) = FORMAT$(Amount, $Mask)
               NEXT
               REDIM PRESERVE sArray(1 TO counter)
               ? JOIN$(sArray(),$CR)
              END FUNCTION
              How long is an idea? Write it down.

              Comment


              • #8
                Passing currency works to get rid of the minus sign in front of "-.00" with some numbers.
                Code:
                MACRO money (curval)= RSET$(FORMAT$(curval, "#.00;-.00;0"),12)
                FUNCTION PBMAIN () AS LONG
                 DIM sArray(1 TO 999) AS STRING
                 LOCAL Counter     AS LONG
                 LOCAL Amount      AS SINGLE
                 FOR Amount = -.40! TO .40! STEP .10!
                  INCR counter
                  sArray(counter) = money(Amount)
                 NEXT
                 REDIM PRESERVE sArray(1 TO counter)
                 ? JOIN$(sArray(),$CR)
                END FUNCTION
                Using currency instead of single precision may be the solution
                Code:
                MACRO money (curval)= RSET$(FORMAT$(curval, "#.00;-.00;.00"),12)
                FUNCTION PBMAIN () AS LONG
                 DIM sArray(1 TO 999) AS STRING
                 LOCAL Counter     AS LONG
                 LOCAL Amount      AS CUR
                 FOR Amount = -.40 TO .40 STEP .10
                  INCR counter
                  sArray(counter) = money(Amount)
                 NEXT
                 REDIM PRESERVE sArray(1 TO counter)
                 ? JOIN$(sArray(),$CR)
                END FUNCTION
                Using @@ does not make the conversion.
                MACRO money (curval)= RSET$(FORMAT$(curval, "#[email protected]@;[email protected]@;[email protected]@"),12)

                This should work if using single precision, but passing currency seems to be the way to go.
                Could use a macro if curval is passed as [email protected]@
                Code:
                FUNCTION money (curval AS SINGLE) AS STRING
                  LOCAL convert AS CUR
                  convert = Curval
                  FUNCTION = RSET$(FORMAT$(convert, "#[email protected]@;[email protected]@;[email protected]@"),12)
                END FUNCTION
                How long is an idea? Write it down.

                Comment


                • #9
                  Because with floating point STEPping from -0.40 it never has a result of zero.

                  Exact multiples of tenths, hundredths, etc. can not be represented in binary.

                  CUR is an integer whose least significant bit represents 0.0001

                  Cheers,

                  (is this a minor hijack of Borje's thread ?)
                  Dale

                  Comment


                  • #10
                    Yes, it has to do with floating point STEP. In my case, I use EXT variables, but no loop or STEP, so it now works like I want it to. Thanks fellows, always interesting conversations that follows questions and replies around here.

                    Comment


                    • #11
                      Originally posted by Borje Hagsten View Post
                      Yes, it has to do with floating point STEP. In my case, I use EXT variables, but no loop or STEP, so it now works like I want it to. Thanks fellows, always interesting conversations that follows questions and replies around here.
                      Using EXT is not a total solution, you still need to be careful with exactly what value you use if you use a long mask (or if you use STR$() )

                      '
                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      FUNCTION PBMAIN () AS LONG
                      LOCAL x,y AS EXT
                      
                      ? FORMAT$(0.9,"0.################")    'returns 0.899999976158142
                      ? FORMAT$(0.9##,"0.################")   'returns 0.9
                      
                      x = 0.9
                      y =0.9##
                      ? FORMAT$(x,"0.################")   'returns 0.899999976158142
                      ? FORMAT$(y,"0.################")   'returns 0.9
                      END FUNCTION
                      '

                      Comment


                      • #12
                        How about this macro using CCUR with FORMAT?
                        Tested with SINGLE, CUR and EXT.
                        Code:
                        #DIM ALL
                        '$Mask = "0.##################;-0.##################;0"
                        $Mask = "#.00;-.00;0" 'season to taste
                        MACRO money(parm) = FORMAT$(CCUR(parm),$Mask)
                        
                        FUNCTION PBMAIN () AS LONG
                         DIM sArray(1 TO 999) AS STRING
                         LOCAL Counter     AS LONG
                         LOCAL Amount      AS SINGLE 'SINGLE, CUR, EXT
                         FOR Amount = -.090! TO .090! STEP .01!
                          INCR counter
                          sArray(counter) = money(Amount)
                         NEXT
                         REDIM PRESERVE sArray(1 TO counter)
                         ? JOIN$(sArray(),$CR)
                        END FUNCTION
                        How long is an idea? Write it down.

                        Comment


                        • #13
                          Originally posted by Mike Doty View Post
                          How about this macro using CCUR with FORMAT?
                          Tested with SINGLE, CUR and EXT.
                          Code:
                          #DIM ALL
                          '$Mask = "0.##################;-0.##################;0"
                          $Mask = "#.00;-.00;0" 'season to taste
                          MACRO money(parm) = FORMAT$(CCUR(parm),$Mask)
                          
                          FUNCTION PBMAIN () AS LONG
                          DIM sArray(1 TO 999) AS STRING
                          LOCAL Counter AS LONG
                          LOCAL Amount AS SINGLE 'SINGLE, CUR, EXT
                          FOR Amount = -.090! TO .090! STEP .01!
                          INCR counter
                          sArray(counter) = money(Amount)
                          NEXT
                          REDIM PRESERVE sArray(1 TO counter)
                          ? JOIN$(sArray(),$CR)
                          END FUNCTION
                          No point to that. If your mask says x number of significant digits, it doesn't matter what type of number you use, that's what you will get. It's only when you use a mask that has more place holders than the number of significant digits in the numeric type that problems arise. With CCUR, you are just doing the same rounding twice, once with the conversion and then again with the mask.

                          Comment


                          • #14
                            I'm trying to use Format$ to present a value, but cannot remember how to fix all cases in one take - or if it's even possible - without using IIF$ work-around. Problem is this - if value is 0, I want to show "0" and if value is for example 0.75 I want to show "0.75". But, while using FORMAT$(0) gives correct "0", same FORMAT$(0.75) without mask gives ".75" without leading 0. So I add a mask, FORMAT$(0.75, "0.##") and get correct "0.75", but then FORMAT$(0, "0.##") result in "0." - with an additional decimal dot that I don't want.

                            The only fix for this I can come up with is using IIF$, like IIF$(0, FORMAT$(0, "0.##"), FORMAT$(0)) where result actually is sort of "wrong" alternative when value is 0 (is then uses second alternative, which gives correct result), but expected correct when there is fractional part (like 0.75), but it surely it should be a better way? I just cannot figure out correct mask to solve this using FORMAT$ alone. Is it even possible? Clear as mud, I know - code below shows problem and results better.

                            Without CCUR returns -0.00 instead of 0
                            Code:
                            #DIM ALL
                            $Mask = "#.00;-#.00;0" 'season to taste
                            MACRO money(parm) = FORMAT$(CCUR(parm),$Mask)
                            FUNCTION PBMAIN () AS LONG
                             DIM sArray(1 TO 999) AS STRING
                             LOCAL Counter     AS LONG
                             LOCAL Amount      AS EXT
                             FOR Amount = [email protected] TO [email protected] STEP [email protected]
                              INCR counter
                              sArray(counter) = money(Amount)
                             NEXT
                             REDIM PRESERVE sArray(1 TO counter)
                             ? JOIN$(sArray(),$CR)
                            END FUNCTION
                            How long is an idea? Write it down.

                            Comment


                            • #15
                              Yes Stuart, but in my case it's a result from parsing out numbers in a text and using VAL(sString) (to EXT variables) for parts - and there it now works like I want it to:
                              '
                              Code:
                              #DIM ALL
                              $Mask = "0.##################;-0.##################;0"
                              FUNCTION PBMAIN () AS LONG
                                ? FORMAT$(VAL("-0.001"),$Mask) + "  " + _
                                  FORMAT$(VAL("0.001"),$Mask) + "  " + _
                                  FORMAT$(VAL("0"),$Mask) ' returns  -0.001  0.001  0
                              END FUNCTION
                              '

                              Comment


                              • #16
                                [QUOTE=Mike Doty;n797418]
                                Without CCUR returns -0.00 instead of 0
                                [QUOTE]

                                If you're going to use
                                FOR Amount = [email protected] TO [email protected] STEP [email protected]

                                wouldn't it be more logical to use
                                LOCAL Amount AS CUR

                                which removes the requirement for CCUR

                                Comment


                                • #17
                                  Yes, unless using SINGLE like in post #7.
                                  How long is an idea? Write it down.

                                  Comment


                                  • #18
                                    Originally posted by Borje Hagsten View Post
                                    Yes Stuart, but in my case it's a result from parsing out numbers in a text and using VAL(sString) (to EXT variables) for parts - and there it now works like I want it to:
                                    Yep, VAL() is evaluating "0.001" as an EXT so you are not getting those spurious digits resulting from converting a lower precision float to Ext..

                                    Comment


                                    • #19
                                      I thought we were working with money when I saw this:
                                      FORMAT$(0.75, "0.##")
                                      Have a good day.
                                      How long is an idea? Write it down.

                                      Comment


                                      • #20
                                        SINGLE needs the CCUR of the parm or 0 returns -0.00.
                                        Code:
                                        #DIM ALL 'money.bas using format$ and ccur
                                        MACRO money(parm) = FORMAT$(parm,"#.00;-#.00;0")
                                        'MACRO money(parm) = FORMAT$(parm,"0.##################;-0.##################;0")
                                        FUNCTION PBMAIN () AS LONG
                                         DIM sArray(1 TO 999) AS STRING
                                         LOCAL Counter     AS LONG
                                         LOCAL Amount      AS SINGLE
                                         FOR Amount = -.090 TO .090 STEP .01
                                          INCR counter
                                          sArray(counter) = money(Amount)
                                         NEXT
                                         REDIM PRESERVE sArray(1 TO counter)
                                         ? JOIN$(sArray(),$CR)
                                        END FUNCTION
                                        How long is an idea? Write it down.

                                        Comment

                                        Working...
                                        X