Results 1 to 6 of 6

Thread: Rounding functions?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Jun 2016
    Location
    Point Cook, Victoria, Australia
    Posts
    572

    Default Re: Rounding functions?

    Hopefully this will help. Because I write mostly accountancy type software, payroll, tax, invoicing, shares, etc... the example below is a basic version of what I use. Of course, most of the time I need an answer rounded to 2 decimal places, hence the use of 100. Three decimal places, use 1000, four decimal places, use 10000, etc...

    If nVal1 = 15,694 and nVal2 = 0.1536 then nResult will equal $2,410.60, otherwise, with a straight multiplication it would calculate out to 2,410.5984.

    Negative numbers are also handled correctly, whether just one is negative, or both are negative. I therefore use this same basic calculation for both debits and credits.

    I've written my own .pkg file which has a whole range of functions similar to this. This function will do the usual round up, but it is also capable of rounding down.

    The function below could have been simplified (read shortened) a bit, but I tend not to do this because in 5 years time when I'm reading this old code, I still wish to understand how it works. So, consequentially, I never write ambiguous code.

    Also take note that DataFlex only calculates left to right, so I am taking advantage of this "feature" by leaving out some very necessary brackets (braces) if you were to calculate this by hand or if you were using some other language.

    The proper way to write it would be (Number(Round(((nVal1 * nVal2) - 0.005) * 100)) / 100).


    Code:
    Get CalcMe 15694 0.1536 True/False to nResult
    
    
    Function CalcMe Number nVal1 Number nVal2 Boolean bRoundDown Returns Number
    
        // Check for 0 result
        If (nVal1 * nVal2 = 0) Function_Return 0
        
        If ((nVal1 < 0 and nVal2 < 0) or (nVal1 > 0 and nVal2 > 0) and bRoundDown) ;
          Function_Return (Number(Round(nVal1 * nVal2 - 0.005 * 100)) / 100)
        Else If (nVal1 < 0 or nVal2 < 0 and bRoundDown) ;
          Function_Return (Number(Round(nVal1 * nVal2 + 0.005 * 100)) / 100)
        Else ;
          Function_Return (Number(Round(nVal1 * nVal2 * 100)) / 100)
    End_Function
    
    
    
    Or using Case if you prefer:
    Code:
    Function CalcMe Number nVal1 Number nVal2 Boolean bRoundDown Returns Number
    Case Begin Case (nVal1 * nVal2 = 0) Function_Return 0 Case Break Case ((nVal1 < 0 and nVal2 < 0) or (nVal1 > 0 and nVal2 > 0) and bRoundDown) Function_Return (Number(Round(((nVal1 * nVal2) - 0.005) * 100)) / 100) Case Break Case (nVal1 < 0 or nVal2 < 0 and bRoundDown) Function_Return (Number(Round(((nVal1 * nVal2) + 0.005) * 100)) / 100) Case Break Case Else Function_Return (Number(Round(nVal1 * nVal2 * 100)) / 100) Case End End_Function

    Last edited by Rachael; 18-Sep-2019 at 05:04 PM.
    Regards,
    Rachael Warlond

    1) When programming, never reinvent the wheel.
    2) If it works, leave well enough alone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •