Announcement

Collapse
No announcement yet.

Format$ problem...

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

  • #21
    Ah yes, sorry Mike. Real problem was how to get consistent format the way I wanted using only Format$, but I can see how my incomplete example could fool anyone into thinking it was about for example money. Anyway, thank you to all for great input - there's is always things to learn for both current and future needs here.

    Comment


    • #22
      No problem. My post #20 needs the CCUR to work with SINGLEs.

      This does work with SINGLEs by adding the CCUR.
      MACRO money(parm) = FORMAT$(CCUR(parm),"0.##################;-0.##################;0")
      How long is an idea? Write it down.

      Comment


      • #23
        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$() )
        You can play with masks for days, but all for naught unless you remember to explicitly assign a type suffix (such as ! # & or @ ) to your numeric literals, as did Mr. McLachlan in post # 11.

        Even CCUR or C <anything> risks rounding depending on the value. Some numbers are rational in decimal but not rational in binary and ALL the floating point types (SINGLE, DOUBLE, EXT) are binary. CUR and CUX are actually integers with scale of 100 or 1000 done at the end.

        Best of all, use the correct data types for your variables and constants EARLY.

        Another option, depending on the format you want.... when formatting a decimal value you may format the TRUNC (), add "." and append the formatted FRAC().

        A MACRO or MACRO FUNCTION can also be easier than trying to get the "three masks" thing working with FORMAT$. .. especially if conditions apply or your desired format is nonstandard (according to PB).

        Also a word of caution.... While you may also use USING$, the formatting strings are NOT the same for FORMAT$ and USING$. Very confusing to some of us (e.g., moi).

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

        Comment


        • #24
          Sure, bring up USING$.
          Here is an example using SWITCH$ with money where 1 = .01
          I prefer USING$ to FORMAT$ and this might be converted without the money.
          Code:
          FUNCTION PBMAIN () AS LONG
           LOCAL x AS LONG, s AS STRING
           FOR x = -10 TO 10 STEP 1
            s = s + Money(x) + $CR
           NEXT
           ? s
          END FUNCTION
          
          FUNCTION Money(BYVAL Pennies AS EXT) AS STRING
           FUNCTION = SWITCH$( _
           pennies = 0, "0", _
           pennies > -100 AND pennies < 100, USING$(".##",pennies * .01##), _
           pennies< > 0,TRIM$(USING$("#,.##",pennies * .01##)))
          END FUNCTION
          How long is an idea? Write it down.

          Comment


          • #25
            You know, Mike D, when you start getting into mu,tiple conditions as you do with SWITCH$(), or even with FORMAT$ for mask selection, I really like to use the continuation underscore and indentting to make it clear what I am doing ...

            Your function from #2, more physical lines

            Code:
            FUNCTION Money(BYVAL Pennies AS EXT) AS STRING
                 FUNCTION = SWITCH$( _
                                                 pennies = 0, _                                ' whole dollars
                                                           "0", _
                                                 pennies > -100 AND pennies < 100, _      ' cents -.00  to +.99
                                                            USING$(".##",pennies * .01##), _
                                                 pennies< > 0,_                                                     ' with money not really possible but for other values very much possible
                                                        TRIM$(USING$("#,.##",pennies * .01##)) _
                                          )
            END FUNCTION
            Plus you can now add comments to each individual line which is each individual condition.

            Yes, this post is TRULY more style than substance... unless you count figuring out what the heck you were doing when you wrote this two years ago as "substance."

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

            Comment


            • #26
              Originally posted by Mike Doty View Post
              SINGLE needs the CCUR of the parm or 0 returns -0.00.
              No, 0 does not return -0,00.

              The floating point value which results from that series of additions using singles is a negative number which rounds to -0.00 to two decimal places. But that number is NOT zero.

              Comment


              • #27
                Originally posted by Michael Mattias View Post
                You know, Mike D, when you start getting into mu,tiple conditions as you do with SWITCH$(), or even with FORMAT$ for mask selection, I really like to use the continuation underscore and indentting to make it clear what I am doing ...

                Your function from #2, more physical lines
                Mike did you continuation underscore with one line per SWITCH$ condition.


                Comment


                • #28
                  Code:
                  FUNCTION PBMAIN () AS LONG
                   ? CHR$( MM(1),$CR,   MM(0),$CR,  MM(-99.99) )
                  END FUNCTION
                  
                  FUNCTION MM(BYVAL Pennies AS EXT) AS STRING
                  FUNCTION = SWITCH$(pennies <> 0, USING$("#.##",pennies), _ 'passed value <> 0
                                     pennies=0,"0")                          'passed value = 0
                  END FUNCTION
                  Mike did you continuation underscore with one line per SWITCH$ condition.
                  Yes, in post #24 without identation or comments.
                  How long is an idea? Write it down.

                  Comment


                  • #29
                    Makes a nice macro
                    Code:
                    MACRO borje(parm)=SWITCH$(parm <> 0, USING$("#.##",parm), parm=0,"0")
                    FUNCTION PBMAIN () AS LONG
                     ? borje(-111.11) + $CR + borje(222.22) + $CR + borje(0) + $CR + borje(3)
                    END FUNCTION
                    How long is an idea? Write it down.

                    Comment


                    • #30
                      ?????
                      If you thought it was money, why use SINGLE at all???????????????? Just stay in Currency or Extended Currency all the way.

                      They can be used for anything with fixed decimal point (2 or 4 places), but "currency" means money.
                      Dale

                      Comment


                      • #31
                        Originally posted by Dale Yarker View Post
                        ?????
                        If you thought it was money, why use SINGLE at all???????????????? Just stay in Currency or Extended Currency all the way.

                        They can be used for anything with fixed decimal point (2 or 4 places), but "currency" means money.
                        Not to mention the LONG -> EXT used in post #24

                        Comment


                        • #32
                          Borje,
                          As you probably know the mask using FORMAT$ may not display whole numbers like you want.

                          Code:
                          $Mask = "0.##################;-0.##################;0" 'original
                          FUNCTION PBMAIN () AS LONG
                           LOCAL e AS EXT
                           e = 9
                           ? FORMAT$(e,"0.##") + $CR +_    '9.
                           USING$("#.##",e)                '9.00
                          END FUNCTION
                          How long is an idea? Write it down.

                          Comment


                          • #33
                            Originally posted by Mike Doty View Post
                            Borje,
                            As you probably know the mask using FORMAT$ may not display whole numbers like you want.

                            Code:
                            $Mask = "0.##################;-0.##################;0" 'original
                            FUNCTION PBMAIN () AS LONG
                            LOCAL e AS EXT
                            e = 9
                            ? FORMAT$(e,"0.##") + $CR +_ '9.
                            USING$("#.##",e) '9.00
                            END FUNCTION
                            You can't equate the # in Format$ and Using$, the rules are very different. Using$ "#.##" is essentially the same as Format$ "0.00". There is no USING$ equivalent to Format$ "#"

                            Comment


                            • #34
                              Whole numbers may need a mask other than $Mask = "0.##################;-0.##################;0" with FORMAT$ was the point.
                              USING$ may be preferred.
                              How long is an idea? Write it down.

                              Comment


                              • #35
                                Yes thank you, I have noticed the problem and changed to use combination IIF$ and FRAC, plus put it all in a function (I like "write once, use many") where I can set number of decimals, if needed. Code below gives me exactly what I want - whole numbers without decimal point when no FRAC and desired number of decimals if FRAC finds fractional part: Am sure there are other ways, but this is enough for current calculator project.
                                '
                                Code:
                                #COMPILE EXE
                                '========================================================================
                                ' Get formatted string without decimal point if no fractional part, or up
                                ' to iDec number of decimals if FRAC finds fractional part. Max iDec = 18.
                                '------------------------------------------------------------------------
                                FUNCTION myFormat$(extVar AS EXT, iDec AS LONG)
                                  STATIC sMask, sTmp AS STRING
                                
                                  IF LEN(sMask) = 0 THEN  ' static, only needs to be done once for better speed
                                      sTmp = STRING$(iDec, "#")
                                      sMask = "0." + sTmp + ";-0." + sTmp + ";0"
                                  END IF
                                
                                  FUNCTION = IIF$(FRAC(extVar), _
                                                  FORMAT$(extVar, sMask), _
                                                  FORMAT$(extVar, iDec))
                                END FUNCTION
                                
                                '======================================================================
                                FUNCTION PBMAIN () AS LONG
                                  ? myFormat$(VAL("-2"),18)      + "  " + _
                                    myFormat$(VAL("2"),18)       + "  " + _
                                    myFormat$(VAL("0"),18)       + $CR  + _
                                    myFormat$(VAL("-2.001"),18) + "  " + _
                                    myFormat$(VAL("2.001"),18)  + "  " + _
                                    myFormat$(VAL("0.001"),18)
                                END FUNCTION
                                '

                                Comment


                                • #36
                                  deleted. "2" looked like it had a . after it, but it is my eyes or monitor.
                                  How long is an idea? Write it down.

                                  Comment


                                  • #37
                                    ? MyFormat(VAL("2.20##"),2) ' 2.2 up to iDec number of decimals
                                    Wouldn't you want "2.20" instead of truncation? It is correct, but just wondered. Maybe not if a calculator.
                                    How long is an idea? Write it down.

                                    Comment


                                    • #38
                                      A good one to use is "RSET USING "0" if you are putting numbers into a fixed size buffer ("print area") and want to ensure vertical alignment.

                                      That can help deal with the difference between "2.2" and "2.20"

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

                                      Comment


                                      • #39
                                        A good one to use is "RSET USING "0" if you are putting numbers into a fixed size buffer ("print area") and want to ensure vertical alignment.
                                        That can help deal with the difference between "2.2" and "2.20"
                                        Thanks, Michael.
                                        That works great!
                                        LSET USING "0" by the way.
                                        Code:
                                        FUNCTION myFormat$(extVar AS EXT, iDec AS LONG)
                                          STATIC sMask, sTmp AS STRING
                                        
                                          IF LEN(sMask) = 0 THEN  ' static, only needs to be done once for better speed
                                              sTmp = STRING$(iDec, "#")
                                              sMask = "0." + sTmp + ";-0." + sTmp + ";0"
                                          END IF
                                        
                                          FUNCTION = IIF$(FRAC(extVar), _
                                                          FORMAT$(extVar, sMask), _
                                                          FORMAT$(extVar, iDec))
                                        END FUNCTION
                                        
                                        '======================================================================
                                        FUNCTION PBMAIN () AS LONG
                                         LOCAL s AS STRING
                                         s = SPACE$(4)
                                         LSET s = MyFormat(VAL("2.20##"),18) USING "0"
                                         ? s  '2.20
                                        END FUNCTION
                                        How long is an idea? Write it down.

                                        Comment


                                        • #40
                                          LSET USING "0" by the way.
                                          Duh.

                                          RSET using $SPC or LSET USING "0" .. of course.

                                          At least I remembered the LSET and RSET statements are handy.
                                          Michael Mattias
                                          Tal Systems Inc. (retired)
                                          Racine WI USA
                                          [email protected]
                                          http://www.talsystems.com

                                          Comment

                                          Working...
                                          X