JavaScript, rounding number after decimal
30/09/2015
etc
Given that, you have to work with many decimal number for example: 0.335, 0.345,
0.2, 96.6666. And then you want to round those number 2 digits after decimal.
The most general way is to used toFix()
. However, the behavior of toFix()
is
not good. Let test with value 0.335
and 0.345
.
The better solution is that, we try to use the first two digit after decimal, the only issue is that, these two digit up/down are depend on the third digit, perhaps the fifth one. Number 44 is to solve this issue. Any number x.a-b-c-d
which has c-d
>= 56, the b
will be round up by one.
A good solution come up with a compact function which can help you use in any time.