Java BigDecimal equals()方法与示例

news/2024/5/19 3:31:10 标签: java, spring, object, python, 反射

BigDecimal类的equals()方法 (BigDecimal Class equals() method)

  • equals() method is available in java.math package.

    equals()方法java.math包中可用。

  • equals() method is used to check whether this BigDecimal and the given object are equal or not and the two objects are equal when both the objects hold the same value and scale.

    equals()方法用于检查BigDecimal和给定对象是否相等,并且当两个对象都具有相同的值和小数位数时,两个对象是否相等。

  • equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    equals()方法是一种非静态方法,仅可通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • equals() method does not throw an exception at the time of comparing.

    equals()方法在比较时不会引发异常。

Syntax:

句法:

    public boolean equals(Object ob);

Parameter(s):

参数:

  • Object ob – represents the object to be compared to this BigDecimal object.

    对象ob –表示要与此BigDecimal对象进行比较的对象。

Return value:

返回值:

The return type of this method is boolean, it returns true when both objects are of "BigDecimal" type and both holds the same value and scale otherwise it returns false.

此方法的返回类型为boolean ,当两个对象均为“ BigDecimal”类型并且都具有相同的值和小数位数时,它返回true ,否则返回false

Example:

例:

// Java program to demonstrate the example 
// of boolean equals(Object ob) method of BigDecimal

import java.math.*;

public class EqualsOfBD {
    public static void main(String args[]) {
        // Initialize two variables first is
        // of "double" and second is of "String"
        // type
        double d = 587.0;
        String str = "587.00";

        // Initialize two BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(d);
        BigDecimal b_dec2 = new BigDecimal(str);


        // By using equals() method, we are comparing
        // this BigDecimal (b_dec1) to the given
        // BigDecimal b_dec2 and here it returns
        // false because they are equal in value
        // but not in terms of scale
        boolean compare = b_dec1.equals(b_dec2);
        System.out.println("b_dec1.equals(b_dec2): " + compare);

        // By using equals() method, we are comparing
        // this BigDecimal (b_dec1) to the given
        // BigDecimal b_dec2 and here it returns
        // true because they both are equal in value
        // and scale
        compare = b_dec1.equals(b_dec1);
        System.out.println("b_dec1.equals(b_dec1): " + compare);
    }
}

Output

输出量

b_dec1.equals(b_dec2): false
b_dec1.equals(b_dec1): true


翻译自: https://www.includehelp.com/java/bigdecimal-equals-method-with-example.aspx


http://www.niftyadmin.cn/n/1255038.html

相关文章

python列表奇数和_在列表python中计算奇数

这是我家庭作业的一部分,我接近最后的答案,但还不完全。我需要写一个函数来计算列表中的奇数。 创建一个递归函数count_odd(l),该函数的唯一参数是一个整数列表。函数将返回奇数列表元素的计数,即不能被2整…

arcgis api 鼠标放上后popup_ARCGIS如何调用天地图遥感底图

目前国家相关系统使用的在线地图大多是天地图的遥感底图,不少用户在往这些系统上报空间数据的时候,很多时候需要在GIS软件查看一下自己上报的SHP文件比如防治责任范围等与天地图遥感是否有偏差。在2019年1月1日前,可以通过安装一些插件后&…

超详细 Hadoop 2.7.4 Installation Procedure

∑工作快两年了,一直没有个自己的技术博客啥的,感到很惭愧,每次遇到问题都是百度大神的文章,今天是周日,我也想写点我一直学习成长中遇到的问题和 解决方法,今天我就拿这个hadoop2.7.4开刀吧,至…

机器学习算法 ——《 KNN 算法——Python语言的实现》实践 (一)

KNN 分类算法原理介绍: 简介:K最近邻(k-Nearest Neighbor,KNN)分类算法是最简单的机器学习算法。KNN算法的指导思想是“近朱者赤,近墨者黑”,由你的邻居来推断出你的类别。采用测量不同特征值之…

c语言产生随机数_单片机如何产生随机数呢?这两个方法可以试试

随机数在单片机的应用中也是很多的,当然产生随机数的方法有很多,当中有一个就是利用单片机定时器,取出未知的定时器THX和TLX的值,再加以运算得到一个规定范围内的随机数值。这做法也是可行的。或者预先写好一个随机数表&#xff0…

java的remove方法_Java ArrayDeque remove()方法与示例

java的remove方法ArrayDeque类remove()方法 (ArrayDeque Class remove() method) Syntax: 句法: public boolean remove(Object obj);public T remove();remove() method is available in java.lang package. remove()方法在java.lang包中可用。 remove() method i…

U盘上不能拷贝大于4G的单个文件问题解决办法

在windows系统上打开Dos命令窗口,插入U盘输入如下指令: conver H: / fs:ntfs 注意:H 是U盘的在电脑上所在的盘符,根据个人情况,自己写入对应的U盘所在盘 使用此命令不需要切换盘符直接运行此代码即可! 以下…

python自动测试网络通断_Python测试网络连通性

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里技术人对外发布原创技术内容的最大平台&…