Created
February 22, 2019 10:18
-
-
Save umpteenthdev/fbb9ee78fbb79947a34a1efb55d4f77c to your computer and use it in GitHub Desktop.
Useful extension for `BigDecimal`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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