I'm using the Add method of decimalJS v10.4.3 to add 2 numbers: 12345678907812569090001
and 5001
.
The result should be 12345678907812569095002
but decimalJS returns 12345678907812569095000
Any suggestions?
It must be because precision is less than the number of digits, so it ends with ...00
var a = new Decimal("12345678907812569090001")
var b = new Decimal("5001")
var c = a.add(b)
console.log (c.toFixed())
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js" integrity="sha512-zPQm8HS4Phjo9pUbbk+HPH3rSWu5H03NFvBpPf6D9EU2xasj0ZxhYAc/lvv/HVDWMSE1Autj19i6nZOfiVQbFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
However should we set precision to a large enough number, it works:
default is Decimal.precision = 20
Decimal.precision = 100
var a = new Decimal("12345678907812569090001")
var b = new Decimal("5001")
var c = a.add(b)
console.log(c.toFixed())
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js" integrity="sha512-zPQm8HS4Phjo9pUbbk+HPH3rSWu5H03NFvBpPf6D9EU2xasj0ZxhYAc/lvv/HVDWMSE1Autj19i6nZOfiVQbFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>