将 BigDecimal 值用作请求变量时出现错误

问题描述 投票:0回答:1

嗨,我在 springboot web 服务中有一个定义为 BigDecimal 的变量,我想用它从数据库获取数据(数据库变量定义为 NUMBER(20,2)。)

如果我输入的 BigDecimal 值等于数据库变量,我创建了一个 if 语句,如果该值没有小数,例如 500,则返回 true。但如果我给出一个十进制数,例如(524.77),它将返回 false。我怎么解决这个问题 ?谢谢

java spring-boot post postman bigdecimal
1个回答
0
投票

如果您发布 Java 代码片段而不是描述它,会更容易为您提供帮助。

以下是如何将 BigDecimal 变量与常量值进行比较的示例:

    @Test
    void compareBigDecimal() {
        BigDecimal check1 = new BigDecimal("524.770");
        BigDecimal check2 = new BigDecimal("524.77");

        // using compareTo() both numbers are equal
        int compared = check1.compareTo(check2);
        assertThat(compared).isEqualTo(0);

        // but using equals() they are not! because of different scale
        boolean equals = check1.equals(check2);
        assertThat(equals).isFalse();
    }
© www.soinside.com 2019 - 2024. All rights reserved.