Announcement

Collapse
No announcement yet.

Suppressing the Decimal point

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

  • Michael Mattias
    replied
    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.

    Leave a comment:


  • Mike Doty
    replied
    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.

    Leave a comment:


  • John Montenigro
    replied
    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...

    Leave a comment:


  • Michael Mattias
    replied
    Code:
    MACRO has_decimals (num) =  FIX(num) <> num
    Last edited by Michael Mattias; 27 Jan 2009, 07:47 AM.

    Leave a comment:


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

    Leave a comment:


  • John Montenigro
    replied
    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

    Leave a comment:


  • Mike Doty
    replied
    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

    Leave a comment:


  • Michael Mattias
    replied
    >has_decimals not defined

    No, it isn't. I figured any rookie could handle that.

    Leave a comment:


  • Mike Doty
    replied
    has_decimals not defined

    Leave a comment:


  • Michael Mattias
    replied
    Code:
    MACRO fmt_number (X) = FORMAT$(X, IIF$(has_decimals, "#.##", "#"))

    Leave a comment:


  • Mike Doty
    replied
    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.

    Leave a comment:


  • Michael Mattias
    replied
    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'

    Leave a comment:


  • Andre Jacobs
    replied
    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

    Leave a comment:


  • Dave Biggs
    replied
    Remove$ the decimal point!

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

    Leave a comment:


  • Arie Verheul
    replied
    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

    Leave a comment:


  • Andre Jacobs
    replied
    Thanks, Dave

    I thought there was a method with format$ or something similar, but your solution is elegant enough.

    Leave a comment:


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

    Leave a comment:


  • Andre Jacobs
    started a topic Suppressing the Decimal point

    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
Working...
X
😀
🥰
🤢
😎
😡
👍
👎