MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.69k stars 742 forks source link

toFraction sometimes returns a string which does not represent an integer #184

Closed barakman closed 6 years ago

barakman commented 6 years ago

Example:

let x = new BigNumber("234.61938355185511345991");
let [n, d] = x.toFraction();
console.log(n); // 2.3461938355185511345991e+22
console.log(d); // 100000000000000000000

It might be better to have this function returning [BigNumber, BigNumber] instead of [string, string].

Version tested: 7.2.1.

MikeMcl commented 6 years ago

Yes, I made that change in decimal.js.

Your preference is noted.

barakman commented 6 years ago

@MikeMcl:

Alternatively, you could change this:

          ? [n1.toString(), d1.toString()]
          : [n0.toString(), d0.toString()];

To this:

          ? [n1.toFixed(), d1.toFixed()]
          : [n0.toFixed(), d0.toFixed()];

I think that will ensure returning a pair of integer-representing strings.

MikeMcl commented 6 years ago

Yes, good idea. I will consider it.

MikeMcl commented 6 years ago

From v8.0.0, toFraction returns [BigNumber, BigNumber]. Thanks for your input.