Tuesday, 22 December 2015

Java - Comparing two BigDecimal numbers

Comparing two BigDecimal Numbers :
java.math.BigDecimal.compareTo(BigDecimal val) compares the BigDecimal Object with the specified BigDecimal value.
JAVA PROGRAM :
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
// create 2 BigInteger objects
        BigInteger big1, big2;
        big1 = new BigInteger("6"); //first number
        big2 = new BigInteger("3"); // second number
        // create int object
        int res;
        // compare bi1 with bi2
res = big1.compareTo(big2);  
if( res == 0 )
System.out.println( "Both values are equal " );
else if( res == 1 )
System.out.println( "First Value is greater " );
else if( res == -1 )
System.out.println( "Second value is greater" );
System.out.println("comparing with zero");
if(big1.compareTo(BigInteger.ZERO)> 0)
{
System.out.println("bi1 is greater ");
}
       }
}


No comments:

Post a Comment