MikeMcl / decimal.js

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

Include zeros option for decimalPlaces function #165

Closed yusufsahinhamza closed 3 years ago

yusufsahinhamza commented 3 years ago

It would be good to give an option to include zeros when using decimalPlaces function just like in the precision .sd([include_zeros]) function.

For example:

x = new Decimal(987.65432100)
x.dp()                         // '6'
y = new Decimal(987.65432100)
y.dp(true)                     // '8'
MikeMcl commented 3 years ago

The number of trailing zeros in the fraction part of the number is not stored when a Decimal is created, so it cannot be retrieved by a method call later.

Thanks for your suggestion but I don't want to make the extensive changes that would be required to support this.

Example workaround:

zeros = s => String(s).replace(/^.*[^0](?=0*$)/, "").length;
v = "987.65432100";
x = new Decimal(v)
x.z = zeros(v); 
console.log(x.z);    // 2
yusufsahinhamza commented 3 years ago

@MikeMcl Yes i also realized that after creating this issue but anyway your example workaround is good to use for me, you can close this issue if you wish, thank you!