Rounding in R Returning Wrong Value <p>Consider the following code using a modified rounding function:</p> <pre><code>round2 = function(x, n) { posneg = sign(x) z = abs(x)*10^n z = z + 0.5 z = trunc(z) z = z/10^n z*posneg } n&lt;-387.9 d&lt;-400 round2(n/d,4) </code></pre> <p>The function should return 0.9698, but it returns 0.9697. This seems to occur during the trunc function when 9698 gets truncated to 9697. Is there another rounding function I can use (besides the default round function) to make this value correct?</p>