How to compare two BigDecimals in Java

1 Answer

0 votes
import java.math.BigDecimal;

public class MyClass {
    public static void main(String args[]) {
        BigDecimal First, Second; 
  
        First = new BigDecimal("97562378.0006"); 
        Second= new BigDecimal("97562378.0007"); 
  
        if (First.compareTo(Second) == 0) { 
            System.out.println("First == Second"); 
        } 
        else if (First.compareTo(Second) == 1) { 
            System.out.println("First > Second"); 
        } 
        else { 
            System.out.println("First < Second"); 
        } 
    }
}




/*
run:

First < Second

*/

 



answered Jul 24, 2022 by avibootz

Related questions

1 answer 133 views
1 answer 96 views
2 answers 136 views
1 answer 205 views
205 views asked Jun 28, 2020 by avibootz
1 answer 126 views
126 views asked Oct 30, 2019 by avibootz
1 answer 147 views
1 answer 212 views
...