-
The java.math.BigDecimal.valueOf(long val) is an inbuilt method in java that translates a long value into a BigDecimal value with a scale of zero. It allows us, the reuse of frequently used BigDecimal values and hence this “static factory method” is provided in preference to a (long) constructor.
Syntax:
public static BigDecimal valueOf(long val)
Parameters: The method accepts a single parameter val of Long data type and refers to the value that needs to be translated into a BigDecimal value.
Return value: The method returns a BigDecimal value of Long val.
Below program illustrates the working of java.math.BigDecimal.valueOf(long val) method:
// Program to demonstrate valueOf(long) method of BigDecimal
import
java.math.*;
public
class
gfg {
public
static
void
main(String[] args)
{
// Creating a Long Object
Long ln =
new
Long(
"745812345678"
);
// Assigning the bigdecimal value of ln to b
BigDecimal b = BigDecimal.valueOf(ln);
// Displaying BigDecimal value
System.out.println(
"The Converted BigDecimal value is: "
+b);
}
}
chevron_rightfilter_noneOutput:The Converted BigDecimal value is: 745812345678
-
The java.math.BigDecimal.valueOf(double val) is an inbuilt method in java that translates a double into a BigDecimal, using the double’s canonical string representation provided by the Double.toString(double) method.
Syntax:
public static BigDecimal valueOf(double val)
Parameters: The method accepts a single parameter val of Double data type and refers to the value that needs to be translated into a BigDecimal value.
Return value: The method returns a BigDecimal value which is equal to or approximately equal to Double val.
Below program illustrates the working of java.math.BigDecimal.valueOf(double val) method:
Program 1:// Program to demonstrate valueOf(double) method of BigDecimal
import
java.math.*;
public
class
gfg {
public
static
void
main(String[] args)
{
// Creating a Double Object
Double d =
new
Double(
"785.254"
);
/// Assigning the bigdecimal value of ln to b
BigDecimal b = BigDecimal.valueOf(d);
// Displaying BigDecimal value
System.out.println(
"The Converted BigDecimal value is: "
+ b);
}
}
chevron_rightfilter_noneOutput:The Converted BigDecimal value is: 785.254
-
The java.math.BigDecimal.valueOf(long unscaledVal, int scale) is an inbuilt method in Java that is used to translate a long unscaled value and an int scale into a BigDecimal. This “static factory method” is provided in preference to a (long, int) constructor because it allows for reuse of frequently used BigDecimal values.
Syntax:
public static BigDecimal valueOf(long unscaledVal, int scale)
Parameters: The method takes two parameters:
- unscaledVal – This is of Long data type and refers to the Unscaled value of the BigDecimal.
- scale- This is of Integer data type and refers to the Scale of the BigDecimal.
Return Value: The method returns a BigDecimal whose value is (unscaledVal × 10-scale).
Below program illustrates the java.math.BigDecimal.valueOf(long unscaledVal, int scale) method:
// Program to demonstrate valueOf(long, int) method of BigDecimal
import
java.math.*;
public
class
gfg {
public
static
void
main(String[] args)
{
// Creating a Long Object
Long ln =
new
Long(
"789654"
);
// Assigning the bigdecimal value of ln to BigDecimal b
// with scale 3
BigDecimal b = BigDecimal.valueOf(ln,
3
);
// Displaying the BigDecimal value
System.out.println(
"The BigDecimal value is "
+ b);
}
}
chevron_rightfilter_noneOutput:The BigDecimal value is 789.654
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#valueOf(double)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.