Rounding Numbers: Difference between revisions

Line 59: Line 59:
[[Image:ROUNDNUM.zip| Download Roundnum.fnc (zipfile)]]
[[Image:ROUNDNUM.zip| Download Roundnum.fnc (zipfile)]]
hold on ... there's an upload restriction preventing this...
hold on ... there's an upload restriction preventing this...
____
// ===========================================================================
//
// The ROUND_NUMBER global function has been designed to return the rounded
// value of a given 'numeric' argument at a specified rounding precision.
//
// The function requires two arguments, as follows:-
//
// (a) The numeric value to be rounded, where the range of valid values is
//    +/- 99,999,999,999,999.99999999.
//
// (b) The rounding precision required.  This must be an integer value in the
//    range -13 to +8.  If a value outside this range is used, the nearest
//    threshold value is substituted.
//
//    Values in the range +1 to +7 perform rounding on the decimal part of
//    the number.  A value of +8 will perform no rounding at all.  A zero
//    value will round to the nearest whole number.  Values -1 to -13 will
//    round progressively to the left of the decimal point (a frequent
//    requirement in financial reporting).
//
// If the result of the rounding produces a value outside the valid numeric
// range (+/- 99,999,999,999,999.99999999) then a zero value is returned.
//
// Assumptions:-  The only assumptions made have been that the range of
// numeric values and the limit of eight decimal places are unlikely to change
// in the forseeable future.
//
// Examples:-
//
//  1st Argument      2nd Argument  Return Value
//  ------------      ------------  ------------
//  123456.87654321        8        123456.87654321
//  123456.87654321        2        123456.88
// -123456.87654321        2        -123456.88
//  123456.98765432        0        123457
// -123456.98765432        -1        -123460
//  123456.98765432        -2        123500
//  123456.98765432        -3        123000
//  987654                -4        990000
//  987654                -5        1000000
//  987654                -6        1000000
//  987654                -7              0
//
// ===========================================================================
Function Round_Number GLOBAL number nVal integer iRnd returns Number
  // If the rounding precision falls in the range -5 to +8, then return:-
  If (iRnd >= -5) Function_Return ;
    (Number(Abs(nVal) / nVal) * 0.5 / (Number(10 ^ (iRnd min 8))) + nVal ;
      / (Number(10 ^ (8 - (iRnd min 8)))) * (Number(10 ^ (8 - (iRnd min 8)))))
  // Else, where the rounding precision is in the range -6 to -13, return:-
  Function_Return ;
    (Number(Abs(nVal) / nVal) * 0.5 * (Number(10 ^ (Abs(iRnd) min 13))) ;
      + nVal / (Number(10 ^ 13)) / (Number(10 ^ ((Abs(iRnd) - 5) min 8))) ;
        * (Number(10 ^ 13)) * (Number(10 ^ ((Abs(iRnd) - 5) min 8))))
End_Function