Skip to content

Convert to / from Java BigDecimal #99

@dankito

Description

@dankito

Hi,

I created some extension methods to convert BigDecimal to / from Java BigDecimal (see below).

Two questions here:

  • Are they correct and do you have any suggestions how to improve them (especially for converting from)?

  • If i give you a pull request with them, would you like to integrate it into the jvmMain branch of your library?

package net.dankito.banking.extensions

import com.ionspin.kotlin.bignum.integer.Sign
import java.math.BigDecimal


fun BigDecimal.toIonspinBigDecimal(): com.ionspin.kotlin.bignum.decimal.BigDecimal {
    return com.ionspin.kotlin.bignum.decimal.BigDecimal.parseString(this.toString()) // TODO: couldn't figure out how to create BigDecimal from unscaledValue and scale
}

fun com.ionspin.kotlin.bignum.decimal.BigDecimal.toJavaBigDecimal(): BigDecimal {
    val converted = BigDecimal.valueOf(this.significand.longValue(), (this.precision - this.exponent - 1).toInt())

    if (this.significand.sign == Sign.NEGATIVE) {
        return converted.negate()
    }

    return converted
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions