Skip to content

Instantly share code, notes, and snippets.

@umpteenthdev
Created February 22, 2019 10:18
Show Gist options
  • Select an option

  • Save umpteenthdev/fbb9ee78fbb79947a34a1efb55d4f77c to your computer and use it in GitHub Desktop.

Select an option

Save umpteenthdev/fbb9ee78fbb79947a34a1efb55d4f77c to your computer and use it in GitHub Desktop.
Useful extension for `BigDecimal`
import java.math.BigDecimal
import java.math.RoundingMode
val BigDecimal.fractionLength: Int
get() =
stripTrailingZeros().toPlainString().substringAfterLast(".", "").length
infix fun BigDecimal.isValueEquals(another: BigDecimal?): Boolean {
if (another == null) return false
return compareTo(another) == 0
}
fun BigDecimal.isValueEquals(another: BigDecimal?, roundToReceiverLength: Boolean): Boolean {
if (another == null) return false
if (!roundToReceiverLength) return isValueEquals(another)
val thisFractionLength = fractionLength
if (thisFractionLength == another.fractionLength) return isValueEquals(another)
return isValueEquals(another.setScale(thisFractionLength, RoundingMode.HALF_EVEN))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment