As pointed out in this post, Mathematica has a special version of Round that
Round rounds numbers of the form x.5 toward the nearest even integer.
A comment by David G suggest that why not have differnt options Direction → {"HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"}
These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below
myRound[x_, d_] := Module[{},
c1 = (1./d)*10;
c2 = 1./d;
theDigit = Last@IntegerDigits@IntegerPart[x*c1];
If[theDigit >= 5,
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2],
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]]
speed test
In[267]:=
myRound[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming
Out[267]= {30.7072, Null}
In[268]:=
Round[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming
Out[268]= {0.285921, Null}
So I am wondering if someone on this site already have developed an efficient toolkit for round matters?
Floor[x+0.5]? – Szabolcs Mar 17 '19 at 11:58myRound[x_, d_] := d*Floor[x/d + 1/2]– Daniel Lichtblau Mar 17 '19 at 13:58myRound[8.121, 0.01]– matheorem Mar 17 '19 at 15:26myRound[8.121,1/100]and numericize afterward. – Daniel Lichtblau Mar 17 '19 at 18:36N@myRound[156.015, 1/100], it gives 156.01, which is wrong – matheorem Mar 18 '19 at 02:05