BigDecimal valueOf() Method in Java
-
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 BigDecimalimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){// Creating a Long ObjectLong ln =newLong("745812345678");// Assigning the bigdecimal value of ln to bBigDecimal b = BigDecimal.valueOf(ln);// Displaying BigDecimal valueSystem.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 BigDecimalimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){// Creating a Double ObjectDouble d =newDouble("785.254");/// Assigning the bigdecimal value of ln to bBigDecimal b = BigDecimal.valueOf(d);// Displaying BigDecimal valueSystem.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 BigDecimalimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){// Creating a Long ObjectLong ln =newLong("789654");// Assigning the bigdecimal value of ln to BigDecimal b// with scale 3BigDecimal b = BigDecimal.valueOf(ln,3);// Displaying the BigDecimal valueSystem.out.println("The BigDecimal value is "+ b);}}chevron_rightfilter_noneOutput:The BigDecimal value is 789.654
- Integer valueOf() Method in Java
- BigInteger valueOf() Method in Java
- DayOfWeek valueOf() method in Java with Examples
- Data Conversion Using valueOf() method in Java
- TimeUnit valueOf() method in Java with Examples
- Locale.FilteringMode valueOf() method in Java with Examples
- Locale.Category valueOf() method in Java with Examples
- Java.lang.Long.valueOf() method with examples
- BigDecimal plus() method in Java
- BigDecimal ulp() Method in Java
- BigDecimal max() Method in Java
- BigDecimal min() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal longValueExact() Method in Java
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#valueOf(double)
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



