So the answer is `no`? ```val x = BigDecimal.ONE v...
# getting-started
k
So the answer is
no
?
Copy code
val x = BigDecimal.ONE
val y = x.setScale(999)

x == y // => false
x.compareTo(y) == 0 // => true
b
Since
BigDecimal.ONE.equals(BigDecimal.ONE.setScale(999))
returns false in plain Java, and Kotlin
==
uses
.equals
,
x==y
is false above
So this really isn't Kotlin-specific
k
I know about that, I was wondering weather there's something like
operator comparesEqual
p
Copy code
x <= y && x >= y
🧌
k
Haha 😄
l
you can override
equals
and make it use
compareTo
k
That's a valid option if you own the type, but it's BigDecimal in my case. As far as I understand, I can't override functions there. The best I can think of right now is extension function
compareEquals
or something like that
a
What you're asking for is an equality check that ignores scale. There isn't a built-in that does that. You'd have to use compareTo.
Copy code
infix fun BigDecimal.equalsUnscaled(other: Any?): Boolean {
    if (this === other) return true
    if (javaClass != other?.javaClass) return false

    other as BigDecimal

    if (compareTo(other) == 0) return true

    return false
}

fun main() {
    val x = BigDecimal.ONE
    val y = x.setScale(5)
    println(x)
    println(y)
    println(x.equalsUnscaled(y))
    println(x equalsUnscaled y)
}

// output
// 1
// 1.00000
// true
// true
k
Thank you!
m
Copy code
infix fun BigDecimal.numericallyEquals(value: BigDecimal) = this.compareTo(value) == 0
This is what my project created for this
The idea for the name came from an AssertJ assertion for doing this exact thing. I think it makes it very clear what it does.
k
Love the name, thanks!
👍 1
m
AssertJ is very good for that, and an incredible assertion library.