scurker / currency.js

A javascript library for handling currencies
https://currency.js.org
MIT License
3.16k stars 142 forks source link

Incorrect value when using `increment: 1` on a negative value that is -.5 #378

Closed magoz closed 2 years ago

magoz commented 2 years ago

I've encountered this unexpected behavior when rounding negative numbers.

currency(10.4, { increment: 1 }).format() // 10
currency(10.5, { increment: 1 }).format() // 11
currency(10.6, { increment: 1 }).format() // 11

currency(-10.4, { increment: 1 }).format() // -10
currency(-10.49, { increment: 1 }).format() // -10  correct
currency(-10.5, { increment: 1 }).format() // -10 <------- this should be -11
currency(-10.51, { increment: 1 }).format() // -11  correct
currency(-10.6, { increment: 1 }).format() // -11

The odd thing is that it only happens with -10.5, not with -10.49 or -10.51.

Am I missing something is this a bug?

scurker commented 2 years ago

Rounding in currently currency.js follows the half round-up method, meaning half values get rounded towards positive infinity. This isn't currently a bug, but there is a request to support multiple types of rounding.