[R] Rounding like in finances

Duncan Murdoch murdoch at stats.uwo.ca
Mon Feb 22 15:38:20 CET 2010


On 22/02/2010 9:06 AM, Oliver wrote:
> Hello,
>
> I need to have a "kaufmaennisches Runden" function.
>
> Is there already something like that?
>
> It means: rounding up the 5, instead of rounding it down.
>
> So, 245.455 would give 245.46
>
> I found no option for this.
> Maybe there is a package for it?

This is a little tricky, because 245.455 is not a representable number 
in base R.  If x is represented as x+epsilon, it will tend to round up, 
if it is x-epsilon, it will tend to round down.  (I say tend to because 
R tries to do banker's rounding, and the math routines attempt to do 
exact calculations even when they are not really possible, so it is hard 
to predict exactly what will happen for a particular number.) There are 
packages for handling rational numbers that could handle it exactly; I 
don't know how flexible they are about rounding.

Duncan Murdoch



More information about the R-help mailing list