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

Set precision for particular Decimal instance #208

Closed frankmangone closed 1 year ago

frankmangone commented 1 year ago

As per the docs, you can only set the precision globally.

Would it not make sense to be able to set the precision for a particular instance or operation?

For example, if precision is set globally to 4, but I have the following operation:

const a = new Decimal(7.2923)
const b = new Decimal(0.0255)
Decimal.add(a, b) // Result: 7.318, Expected: 7.3128

This is NOT what I want, but I'm setting the precision in my application in such a way that I cannot control its current value accurately. It would be much more desirable to control it at instance level (on new Decimal declaration).

Would it be possible to add it as a constructor option?

frankmangone commented 1 year ago

Nevermind, I can use clone to solve this!

const Dec = Decimal.clone({ precision: 16 })
Dec.add(a, b) // result: 7.3128