Announcement

Collapse
No announcement yet.

Suppressing the Decimal point

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

  • Suppressing the Decimal point

    I am doing a Celsius to Fahrenheit conversion,
    the problem is that the converted figure must include only 2 characters beyond the decimal point (if any) and nothing if there are no decimals.

    With the code below, it is doable, except that when the answer is a whole number (No decimals), then it still displays the decimal point.
    How can I suppress the decimal point in this case?

    Examples:-
    Format$(35.1234, "####.##") ' gives "35.12" - fine
    Format$(35.1, "####.##") ' gives "35.1" - fine
    Format$(35, "####.##") ' gives "35." - Not fine, must give "35" (with no decimal point)



    Code:
            Case %wm_command
                Select Case As Long Cb.Ctl
                    Case %IDC_BUTTON3              ' Convert from C to F
                        Control Get Text Cb.Hndl, %IDC_TEXTBOX1 To sTxt$
                        sFmTxt$ = Str$((9/5)*(Val(sTxt$))+32)
                        Control Set Text Cb.Hndl, %IDC_TEXTBOX2, Format$(Val(sFmTxt$), "####.##")
    
                    Case %IDC_BUTTON4              ' Convert from F to C
                        Control Get Text Cb.Hndl, %IDC_TEXTBOX2 To sTxt$
                        sFmTxt$ = Str$((5/9)*(Val(sTxt$)-32))
                        Control Set Text Cb.Hndl, %IDC_TEXTBOX1, Format$(Val(sFmTxt$), "####.##")
    
                End Select ' Cb.Ctl
    Andre Jacobs
    "The problem with Computers is that they do what you tell them, not what you want them to"

  • #2
    A$=Format$(Val(sFmTxt$), "####.##")
    If Right$(A$,1)="." then A$=A$(1,Len(A$)-1)
    Control Set Text Cb.Hndl, %IDC_TEXTBOX2, A$
    Dave.

    You're never too old to learn something stupid.

    Comment


    • #3
      Thanks, Dave

      I thought there was a method with format$ or something similar, but your solution is elegant enough.
      Andre Jacobs
      "The problem with Computers is that they do what you tell them, not what you want them to"

      Comment


      • #4
        Or try the following

        Code:
            Local A As Single
            Local FormatString As String
           
            A = 35.1783744
          
            A = Round(A,2)       ' Round to the desired number of digits
            
            FormatString = IIf$(Frac(A) = 0, "##", "##.##")     ' Test for decimals
            
            Print Using$(FormatString,A)
        Arie Verheul

        Comment


        • #5
          Remove$ the decimal point!

          Code:
          Control Set Text Cb.Hndl, %IDC_TEXTBOX2, Remove$(Format$(sFmTxt$, "0.00"), ".00")
          Rgds, Dave

          Comment


          • #6
            Thank you fellows,

            You got my brain turning over again, with the nudges in the right direction

            I ended up with:
            Code:
            Control Get Text Cb.Hndl, %IDC_TEXTBOX1 To sTxt$
            sFmTxt$ = Str$((9/5)*(Val(sTxt$))+32)
            Control Set Text Cb.Hndl, %IDC_TEXTBOX2, RTrim$(Format$(Val(sFmTxt$), "##.##"), ".")
            serves my purpose just fine
            Andre Jacobs
            "The problem with Computers is that they do what you tell them, not what you want them to"

            Comment


            • #7
              These work good for money.... which by sheer good luck (yours) I choose to display with two decimals..
              Code:
              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'
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Money:
                ? USING$("#.##",Pennies&/100)
                Noticed Michael's second and third macro put negative sign in the back and the first macro puts the negative sign in the front.
                If the number is positive there is a trailing space in the 2nd and third macro.
                USING$ with LONGs never have a rounding issue, only a range limitation.

                Comment


                • #9
                  Code:
                  MACRO fmt_number (X) = FORMAT$(X, IIF$(has_decimals, "#.##", "#"))
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    has_decimals not defined

                    Comment


                    • #11
                      >has_decimals not defined

                      No, it isn't. I figured any rookie could handle that.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Code:
                        #COMPILE EXE
                        #DIM ALL
                        MACRO fmt_number (X, has_decimals) = FORMAT$(X, IIF$(has_decimals, "#.##", "#"))
                        FUNCTION PBMAIN () AS LONG
                        ? fmt_number(9.01,1)
                        END FUNCTION

                        Comment


                        • #13
                          Just curious - would this be an improvement? It would avoid having the caller determine and tell the function whether the number "has_decimals"...
                          -jhm

                          Code:
                          MACRO fmt_number (X) = FORMAT$(X, IIF$(INSTR(X,"."), "#.##", "#"))
                          
                          FUNCTION PBMAIN () AS LONG
                            ? fmt_number(YourNumber)
                          END FUNCTION

                          Comment


                          • #14
                            No.
                            It doesn't compile.
                            The purpose is to tell the function which way you want the result.

                            Comment


                            • #15
                              Code:
                              MACRO has_decimals (num) =  FIX(num) <> num
                              Last edited by Michael Mattias; 27 Jan 2009, 07:47 AM.
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                Originally posted by Mike Doty View Post
                                No.
                                It doesn't compile.
                                The purpose is to tell the function which way you want the result.
                                Got it to compile by inserting the STR$() conversion.

                                MACRO fmt_number (X) = FORMAT$(X, IIF$(INSTR(STR$(X),"."), "#.##", "#"))

                                Difference in coding style? I'd rather the program figure out what's needed from what's there, rather than me having to know and tell it...

                                Comment


                                • #17
                                  No, it is not a difference in coding style.
                                  The program doesn't know what function to use.
                                  If you always want your number one way, one function is all that is needed.
                                  Michael wrote the MACRO has_decimals to give you the ability to call any function.
                                  Code:
                                  #COMPILE EXE
                                  #DIM ALL
                                  MACRO has_decimals (num) =  FIX(num) <> num
                                  MACRO fmt_number (X) = FORMAT$(X, IIF$(INSTR(STR$(X),"."), "#.##", "#"))
                                  FUNCTION PBMAIN () AS LONG
                                    LOCAL num AS SINGLE
                                    num = 123.456      'answer rounded
                                    num = 123
                                   
                                  'The program doesn't know if you want a whole number unless you tell it
                                  'That is why Michael gave you another macro function
                                    IF has_decimals(num) THEN
                                      ? fmt_number(num)
                                    ELSE
                                      ? "Do you want it in another format?
                                    END IF
                                    SLEEP 3000
                                  END FUNCTION
                                  Last edited by Mike Doty; 27 Jan 2009, 01:58 PM.

                                  Comment


                                  • #18
                                    Actually I wrote it so you never have to test the number for decimals in-line at all:
                                    Code:
                                    MACRO has_decimals (num) = FIX(num) <> num 
                                    MACRO Fmt_num (num)       = FORMAT$ (num, IIF$(has_decimals(num), "##.##", "#")) 
                                    ...
                                     
                                       PRINT fmt_num(num)   ' prints with decimals if it has any else without.
                                    I was just trying to show how combining FORMAT$ with IIF$ can make your life easy.

                                    Apparently not as desireable a goal as I had thought; "The long, hard way" remains immensely popular. (see also: http://www.powerbasic.com/support/pb...ad.php?t=39710)

                                    MCM
                                    Last edited by Michael Mattias; 27 Jan 2009, 03:51 PM.
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment

                                    Working...
                                    X