MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.35k stars 480 forks source link

What is the difference between `ROUND_HALF_UP` and `ROUND_HALF_CEIL`? #202

Closed PaulRBerg closed 2 years ago

PaulRBerg commented 2 years ago

The docs define ROUND_HALF_UP like this:

Rounds towards nearest neighbour. If equidistant, rounds away from zero.

And ROUND_HALF_CEIL like this:

Rounds towards nearest neighbour. If equidistant, rounds towards Infinity.

I just don't see what is the difference between "away from zero" and "towards Infinity"? Using either rounding mode seems to produce the same outputs, at least for the inputs that I have used.

In what cases would the outputs diverge between these two different rounding modes?

MikeMcl commented 2 years ago

Rounding away from zero in the case of a negative number means rounding towards negative infinity rather than towards infinity.

PaulRBerg commented 2 years ago

Oh, I see. Thanks for explaining!