Round Function Behaviour

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Konflict3
Posts: 4
Joined: Sun Jun 09, 2013 10:57 pm

Round Function Behaviour

Post by Konflict3 » Sat Apr 11, 2015 10:43 pm

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:

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Round Function Behaviour

Post by WaltBrown » Sun Apr 12, 2015 9:19 am

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".
Walt Brown
Omnis traductor traditor

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Round Function Behaviour

Post by WaltBrown » Sun Apr 12, 2015 9:26 am

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".
Walt Brown
Omnis traductor traditor

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6518
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Round Function Behaviour

Post by dunbarx » Mon Apr 13, 2015 3:30 am

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

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Round Function Behaviour

Post by WaltBrown » Mon Apr 13, 2015 10:12 am

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.
Walt Brown
Omnis traductor traditor

Post Reply

Return to “Mac OS”