Round function

klaus

Expert
Licensed User
Longtime User
Hello Erel
In the german forum Bobbes asked some help about the Round function and got an answer from specci48.
http://www.b4x.com/forum/showthread.php?t=1048

In his answer I wondered that the Round function works like
if > 5 then round up
else round down
Round(4.5,0) > 4

in my mind it should work like
if>=5 then round up
else round down
Round(4.5,0) > 5

as it does with the Format function.

I tried it in VisualBaisc it's the same.

Is there any reason for this behaviour.
I have joined a small example to show the difference.

Best regards
Klaus
Switzerland
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4ppc uses the .Net Framework Round function.
Here is a quote from .Net documentation that explains this behavior:
Return Value

The whole number nearest a. If a is halfway between two whole numbers, one of which by definition is even and the other odd, then the even number is returned.
Remarks

The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding.
 

Henry

Member
Licensed User
Good to know :)

Klaus, Round and Format are function for different purposes - it's ok that they work
different.
 
Top