Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using 0 (unlimited) precision with a scale yields 0 when the number of digits exceeds the scale #267

Open
joffrey-bion opened this issue Sep 11, 2023 · 3 comments
Assignees

Comments

@joffrey-bion
Copy link

joffrey-bion commented Sep 11, 2023

Describe the bug

When using 0 (unlimited) precision with a scale, I get 0 (with weird exponents) each time I have numbers that have more digits than the scale.

To Reproduce

val mode = DecimalMode(roundingMode = RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, scale = 4)
println(BigDecimal.fromInt(123, decimalMode = mode)) // 1.23E+2 (OK)
println(BigDecimal.fromInt(1234, decimalMode = mode)) // 1.234E+3 (OK)
println(BigDecimal.fromInt(12345, decimalMode = mode)) // 0.0E+4 (why?)
println(BigDecimal.fromInt(12345678, decimalMode = mode)) // 0.0E+16 (why?)

Expected behavior

In this setup, I would expect the scale to be the only thing that's bounded, so that if digits after the decimal point keep going, we round them to the scale. I would expect 12345 to yield 1.2345E+4 and 12345678 to yield 1.2345678E+7.

Platform

  • JVM
@joffrey-bion
Copy link
Author

joffrey-bion commented Sep 11, 2023

Interestingly, when directly using a decimal number, it doesn't give 0:

val mode = DecimalMode(roundingMode = RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, scale = 4)
println("12345678.1234567".toBigDecimal(decimalMode = mode)) // 1.23456781235E+7 (OK)

@ionspin
Copy link
Owner

ionspin commented Sep 11, 2023

Hi @joffrey-bion, thanks for reporting, I'll look into this.

@ionspin ionspin self-assigned this Sep 11, 2023
@mihbor
Copy link

mihbor commented Sep 23, 2023

I'm getting a potentially related issue when dividing with DecimalMode with unlimited precision and a fixed scale:
Dividing 200.5230146281866563757715403019550414480770532190116073 by 1111.99009855
with DecimalMode(roundingMode = RoundingMode.ROUND_HALF_CEILING, scale = 8)
yields: 0.00000000

Platform

  • JS
    Version
  • 0.3.8

Workaround: use big but limited decimalPrecision, e.g.
DecimalMode(decimalPrecision = 64, roundingMode = RoundingMode.ROUND_HALF_CEILING, scale = 8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants