Skip to content

divide with DecimalMode decimalPrecision = 0 returns zero #331

@ericksli

Description

@ericksli

Describe the bug

I tried to do a division and set the DecimalMode with scale and roundingMode but found out that the result is zero which scale is for the exponent of the scientific notation. If I set the decimalPrecision to a larger number then I can get the correct result.

To Reproduce

import com.ionspin.kotlin.bignum.decimal.DecimalMode
import com.ionspin.kotlin.bignum.decimal.RoundingMode
import com.ionspin.kotlin.bignum.decimal.toBigDecimal
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import java.math.BigDecimal

class BigDecimalTest {
    @Test
    fun kmp1() {
        "460.0".toBigDecimal().divide(
            other = 40.toBigDecimal(),
            decimalMode = DecimalMode(scale = 2, roundingMode = RoundingMode.FLOOR),
        ).shouldBe("11.5".toBigDecimal()) // Fail
        // Expected :1.15E+1
        // Actual   :0.0E+2
    }

    @Test
    fun kmp2() {
        "460.0".toBigDecimal().divide(
            other = 40.toBigDecimal(),
            decimalMode = DecimalMode(decimalPrecision = 6, scale = 2, roundingMode = RoundingMode.FLOOR),
        ).shouldBe("11.5".toBigDecimal()) // Pass
    }

    @Test
    fun java() {
        BigDecimal("460.0").divide(BigDecimal("40"), 2, java.math.RoundingMode.FLOOR)
            .shouldBe(BigDecimal("11.50")) // Pass
    }
}

Expected behavior

Return value should not be zero if the actual result is not zero even decimalPrecision = 0 and the scale should be the number of decimal places of the result.

Platform

  • JVM OpenJDK Runtime Environment Microsoft-7626293 (build 17.0.7+7-LTS)

Additional context
Nil.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions