Page 1 of 1

Round Function Behaviour

Posted: Sat Apr 11, 2015 10:43 pm
by Konflict3
Hi,

I just can't get my head around builtin "round" function.

As expected:
round(49,-2) yields 0
round(51,-2) yields 100

but
round(50,-2) yields 0

or
round(55,-1) yields 50

similarly
round(5,-1) yields 0

Function description explains:
The round function performs financial-style rounding. If the number is exactly halfway between two numbers, round always rounds the number up if positive, down if negative.
Whats going on here :shock:

Re: Round Function Behaviour

Posted: Sun Apr 12, 2015 9:19 am
by WaltBrown
This might be a bug. I looked at a few descriptions of financial rounding and it should probably round up if the preceding number is odd and round down when it is even ("rounding to even"), in the case of a tie-breaker (ie "5"). LC seems to always round down the absolute value (ie 65 rounds to 60, and -65 rounds to -60). I think that financially (at least based on what I read - consult a GAAP expert to verify), 65.55 should round to 65.6 and 65.45 should round to 65.4. I couldn't find any info on negative financial values, but I assume the same odd/even rule would apply.

It could just be seen as a documentation bug - instead of "financial" rounding ("rounding half to even"), it is doing "rounding half to zero" (good discussion in Wikipedia on "Rounding" - also indicates this is actually an option in most math libraries).

Maybe we need a property of "roundingHalfStyle" - default to "roundHalfToEven" as a default would match the documentation (and what I could find on financial sites), alternates could be "roundHalfToOdd", "roundHalfUp", "roundHalfDown", "roundHalfToZero", and "roundHalfToInfinity".

Re: Round Function Behaviour

Posted: Sun Apr 12, 2015 9:26 am
by WaltBrown
Note that the statRound DOES discuss this:
The statRound function performs statistical rounding. If the number is exactly halfway between two numbers, statRound rounds the number to the nearest even integer. (To round off numbers such numbers upward, as is the convention for financial applications, use the round function instead.) Because of this, a set of numbers rounded with statRound is not skewed upward, the way it is with the round function. Therefore, you should use statRound when statistical accuracy is required.
But if you are rounding say to tenths, statRound does NOT round to even tenths, it gives exactly the same result as "round".

Re: Round Function Behaviour

Posted: Mon Apr 13, 2015 3:30 am
by dunbarx
Hi.

Unless I am misunderstanding, round(50,-2) should indeed give a "0". It is the value when you want only that portion of the argument that lies two to the left of the decimal, which is nothing at all, or rather, the digit two to the left of the decimal in "000050.000".

Craig Newman

Re: Round Function Behaviour

Posted: Mon Apr 13, 2015 10:12 am
by WaltBrown
My concern is the use of the word "financial" in the definitions of "round" and "statRound", and in the description of "statRound, where the first paragraph describes rounding to integers while the second paragraph describes rounding to fractions - "statRound" acts "half to even" rounding to integers, but "half toward zero" rounding to fractions, when tested using MessageBox and when I coded it.