Rounding Numbers
Rounding Numbers
The following has been taken from the DAW newsgroup, User Contributed Oct 6 2000
MARF - A Mutually Agreed Rounding Function?!
Aside from UI issues, OS and hardware problems, perhaps one of the most frequently recurring themes in both the old DAC CompuServe Forum, and now here, has been 'How to perform proper rounding of numeric values'.
Interestingly, each time the topic reappears as a new thread, nobody ever refers the questioner back to the previous threads on the subject. It is always a great opportunity to air one's own (usually strictly limited) solution to the problem!
Well, I propose that we use this message thread to, if possible, finally agree upon a proper specification and solution to this problem ... the one that DAC forgot to supply us ... and finally lay this ghost to rest!
So let me kick-off.
Here is what I expect of a proper Rounding function:-
(a) It should be able to cope with all numbers in the DataFlex numeric range ie. ± 99,999,999,999,999.99999999.
(b) Rounding should occur when the digit to the right of the rounding position is 5 or above. I take this to be a reasonably 'international' convention.
(c) It should be possible to round to any degree of precision. In other words, it should be capable of rounding to any position to the left of the decimal delimiter as well as to the right of it. This, after all, is a frequent requirement in financial reporting. So, for example, if I wish to round to the nearest thousand, then a value of 72637.54 should be returned as 73000.
I have attached a file named ROUNDNUM.FNC which contains a global function
that attempts to meet these requirements. Use of the function, named
ROUND_NUMBER, is fully documented therein.
My approach is substantially based on some code originally presented by Hendrick van Niekerk in a CompuServe message in October 1997. I note that he has repeated the code in a message reply in the VDF NG on 11-Jun-2000.
I prefer Hendrick's method to all the others I have yet seen. His code is actually contained in a macro command named ROUND THEN. Basically all I have done is to convert his macro into a global function and then extended the scope of the code to provide for rounding to a more flexible degree of precision.
So ... well ... thanks Hendrick!
Regards,
Michael Pilsworth
Download Roundnum.fnc (zipfile) hold on ... there's an upload restriction preventing this...