BigDecimal Detailed explanation :

*
Java stay java.math Provided in the package API class BigDecimal, Used for more than 16 The number of significant bits is calculated accurately . Double precision floating point variable double Can handle 16 Bit significant number . in application , We need to calculate and process larger or smaller numbers .float and double It can only be used for scientific calculation or engineering calculation , In Business Computing java.math.BigDecimal.BigDecimal What you create is an object , We can't use traditional methods +,-,
,/ And other arithmetic operators directly carry out mathematical operations on their objects , The corresponding method must be called . The parameters in the method must also be BigDecimal Object of . Constructors are special methods of classes , Dedicated to creating objects , Especially for objects with parameters .
equals The method compares the value with the accuracy , and compareTo Precision is ignored .

equals Source code :
public boolean equals(Object anObject)
{//name2 afferent equals method ,anObject point name2,name2 It's the target of transformation
if (this == anObject) {//this Anaphora call equals Methodological name1,name1 and name2 The address is different , Continue to run down return
true; } if (anObject instanceof String) {// This is for judgment name2 Is it String Class or its subclasses , Here it is , Continue to run
String anotherString = (String)anObject;// Transformation under the target int n =
value.length;// As can be seen in the compiler value Is a global variable , The former is omitted this( Namely n = this.value), For measurement name1 The length of if
(n == anotherString.value.length) {// judge name1 and name2 Is the length of the strings pointed to equal , It's equal here , Continue to run char
v1[] = value;// take name1 The string pointed to is stored in the character array v1 char v2[] =
anotherString.value;// take name1 The string pointed to is stored in the character array v2 int i = 0; while (n-- != 0)
{// Using the cycle , Comparing two strings character by character , If there is any difference , return false, Otherwise, return true if (v1[i] != v2[i]) return false;
i++; } return true; } } return false; }
Here's how java.math.BigDecimal.compareTo() Declaration of method

public int compareTo(BigDecimal val)

parameter :

val-- It's the same thing BigDecimal Value of comparison .

Return value :

This method , If BigDecimal Is less than val return -1, If BigDecimal Is greater than val return 1, If BigDecimal Is equal to val return 0

The following example demonstrates math.BigDecimal.compareTo() Method usage .

public class BigdecimalTest {

public static void main(String[] args) {
BigDecimal z1 = new BigDecimal("0"); BigDecimal z2 = new BigDecimal("0.0");
System.out.println(z1.equals(z2)); System.out.println(z1.compareTo(z2)); }}
Output results :

1.false

2.0

Technology