ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
345 stars 41 forks source link

scale error, #222

Closed treeseedm closed 2 years ago

treeseedm commented 2 years ago

scale error, for example var r = 156.10.toBigDecimal().toStringExpanded() assertEquals("156.10", r) the result: expected:<156.1[0]> but was:<156.1[]> Expected :156.10 Actual :156.1 I expected keep two decimals,but “toStringExpanded “ will remove "0",then result will be error. please help me,thank you

ionspin commented 2 years ago

Hi @treeseedm, you are making an incorrect presumption that 156.10 when converted to BigDecimal, will have a scale 2, or decimal precision of 5, it will actually have scale 1 and decimal precision of 4. Here is the relevant code when parsing floating point strings (or doubles/floats they all get converted to strings anyways)

...
                            var rightLastNonZero = right.indexOfLast { it != '0' }

                            if (rightLastNonZero == -1) {
                                rightLastNonZero = right.length - 1
                            }
                            val leftTruncated = left.substring(leftFirstNonZero, left.length)
                            val rightTruncated = right.substring(0, rightLastNonZero + 1)
...

But even if you had a number with proper scale I don't want to add formmating support to the library at this moment, as I don't have time nor need for it. Contributions are always welcome, unfortunately your pull request did not fix this issue and would introduce additional issues, so I can't accept it.

Cheers!