BigDecimal Equals

ID

kotlin.bigdecimal_equals

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

reliability, suspicious-comparison

Description

Reports BigDecimal.equals(other) calls and Kotlin structural equality (==, !=) between two BigDecimal values. BigDecimal.equals also compares the scale of the two values, so BigDecimal("1.0") and BigDecimal("1.00") are reported as unequal even though they represent the same number.

Rationale

import java.math.BigDecimal

val a = BigDecimal("1.0")
val b = BigDecimal("1.00")

a.equals(b)   // FLAW — false, scales differ
a == b        // FLAW — Kotlin == calls equals(); same trap

Kotlin’s structural-equality operator dispatches to equals, so the operator-form has exactly the same behaviour as a direct call. The mistake is silent at compile time, surfaces as a comparison that "never matches" when one side comes from a string parse and the other from arithmetic, and is notoriously hard to diagnose from logs.

Remediation

Use BigDecimal.compareTo, which compares values only:

val a = BigDecimal("1.0")
val b = BigDecimal("1.00")

a.compareTo(b) == 0    // OK — true
a.compareTo(b) != 0    // OK — false

If you do need scale to be part of the comparison (rare — typically only for display or schema validation), call equals and add a comment explaining the requirement.