java.math.BigDecimal Class
gives its client finish control over adjusting conduct, compelling the client to expressly indicate an adjusting conduct for operations capable of discarding precision. The java.math.BigDecimal Class
is used to perform operations for hashing, comparing. Following is the syntax declaration of the java.math.BigDecimal Class.
[java]
public class BigDecimal
extends Number
implements Comparable<BigDecimal>
[/java]
java.math.BigDecimal Class
.
Constructors | Description |
---|---|
BigDecimal(BigInteger) | To change an integer to decimal. |
BigDecimal(double) | To change the double to decimal. |
java.math.BigDecimal Class
.
Methods | Description |
---|---|
abs() | To give back a BigDecimal whose esteem is the absolute estimation of this BigDecimal. |
compareTo(BigDecimal) | To contrast the BigDecimal and the predetermined BigDecimal. |
doubleValue() | BigDecimal will be converted to a double. |
hashCode() | To return the hash code of the BigDecimal. |
java.math.BigDecimal.abs()
method is to give back a BigDecimal whose esteem is the absolute estimation of this BigDecimal. Following is the syntax declaration of the method.
[java]public BigDecimal abs()[/java]
DemoBigDecimal.java
[java]
package com.SPlessons;
import java.math. *;
public class DemoBigDecimal {
public static void main(String[] args) {
// create 2 BigDecimal objects
BigDecimal bg1, bg2;
// assign value to bg1
bg1 = new BigDecimal("-79");
// value before applying abs
System.out.println("Value is " + bg1);
// assign absolute value of bg1 to bg2
bg2 = bg1.abs();
// print bg2 value
System.out.println("Absolute value is " + bg2);
}
}
[/java]
The java.math.BigDecimal.abs()
is used to display the absolute value.
Output: Now compile the code result will be as follows.
[java]
Value is 79
Absolute value is 79
[/java]
java.math.BigDecimal.compareTo(BigDecimal val)
method is to contrast the BigDecimal and the predetermined BigDecimal. Following is the syntax declaration of the method.
[java]public int compareTo(BigDecimal val)[/java]
DemoBigDecimal.java
[java]
package com.SPlessons;
import java.math. *;
public class DemoBigDecimal {
public static void main(String[] args) {
// create 2 BigDecimal objects
BigDecimal bg1, bg2;
bg1 = new BigDecimal("100");
bg2 = new BigDecimal("120");
//create int object
int res;
res = bg1.compareTo(bg2); // compare bg1 with bg2
String str1 = "Both values are equal ";
String str2 = "First Value is greater ";
String str3 = "Second value is greater";
if( res == 0 )
System.out.println( str1 );
else if( res == 1 )
System.out.println( str2 );
else if( res == -1 )
System.out.println( str3 );
}
}
[/java]
In the above example, the developer is comparing two values that are 100 and 120. According to the logic the second value large so it will be printed.
Output: Now compile the code result will be as follows.
[java]
Second value is greater
[/java]
java.math.BigDecimal.doubleValue()
method is to convert bigdecimal value as double. Following is the syntax declaration of the method.
[java]public double doubleValue()[/java]
DemoBigDecimal.java
[java]
package com.SPlessons;
import java.math. *;
public class DemoBigDecimal {
public static void main(String[] args) {
// create a BigDecimal object
BigDecimal bg;
// create a Double object
Double d;
bg=new BigDecimal("4321");
// assign the converted value of bg to d
d=bg.doubleValue();
String str = "Double value of " + bg + " is " + d;
// print d value
System.out.println( str );;
}
}
[/java]
The value of the BigDecimal will be changed as the double value.
Output: Now compile the code result will be as follows.
[java]
Double value of 4321 is 4321.0
[/java]
java.math.BigDecimal.hashCode()
method is To return the hash code of the BigDecimal. Following is the syntax declaration of the method.
[java]public int hashCode()[/java]
DemoBigDecimal.java
[java] // create 3 BigDecimal objects
BigDecimal bg1, bg2, bg3;
// create 3 int objects
int i1, i2, i3;
bg1 = new BigDecimal("125");
bg2 = new BigDecimal("125.50");
bg3 = new BigDecimal("125.80");
// assign the HashCode value of bg1, bg2, bg3 to i1, i2, i3
// respectively
i1 = bg1.hashCode();
i2 = bg2.hashCode();
i3 = bg3.hashCode();
String str1 = "HashCode of " + bg1 + " is " + i1;
String str2 = "HashCode of " + bg2 + " is " + i2;
String str3 = "HashCode of " + bg3 + " is " + i3;
// print i1, i2, i3 values
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );[/java]
Every number will have its own hash value, in the above example, the developer has given three integers to get the hashcode.
Output: Now compile the code result will be as follows.
[java]
HashCode of 135 is 4185
HashCode of 132.50 is 410752
HashCode of 185.80 is 575982
[/java]
java.math.BigDecimal.intValue()
is used to return BigDecimal value as int value.java.math.BigDecimal.intValueExact()
method is used to search for lost data.java.math.BigDecimal.equals()
method is used to compare two objects equally.