Java BigDecimal valueOf() Method with Example

BigDecimal Class valueOf() method: Here, we are going to learn about the valueOf() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 08, 2020

BigDecimal Class valueOf() method

Syntax:

    public static BigDecimal valueOf (double d);
    public static BigDecimal valueOf (long l);
    public static BigDecimal valueOf (long unsc_val , int sc_val);
  • valueOf() method is available in java.math package.
  • valueOf (double d) method is used to convert the given double value into a BigDecimal.
  • valueOf (long l) method is used to convert the given long value into a BigDecimal.
  • valueOf (long unsc_val , int sc_val) method is used to convert the given long unscaled value and an integer value into a BigDecimal.
  • These methods may throw an exception at the time of returning the value of the given parameter.
    NumberFormatException: This exception may throw when the given parameter is not finite.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, valueOf(double d),
    • double d – represents the double value to be converted to a BigDecimal.
  • In the first case, valueOf (long l),
    • long l – represents the long value to be converted to a BigInteger.
  • In the first case, valueOf (long unsc_val, int sc_val),
    • long unsc_val – represents the unscaled value of this BigDecimal.
    • int sc_val – represents the scale of this BigDecimal.

Return value:

In all the cases, the return type of the method is BigDecimal,

  • In the first case, it returns the converted double value to a BigDecimal.
  • In the second case, it returns the converted long value to a BigDecimal.
  • In the third case, it returns the BigDecimal and its value is calculated by using [(unsc_val) * 10 pow (-sc)].

Example:

// Java program to demonstrate the example 
// of valueOf() method of BigDecimal

import java.math.*;

public class ValueOfOfBD {
    public static void main(String args[]) {
        // Instantiates three variables l_val
        // d_val, unscale_val
        long l_val = 125487312456l;
        double d_val = 1245871.12345;
        long unscale_val = 123458745123l;

        // converts the given long value into
        // a BigDecimal and store it in a variable
        // named value_of
        BigDecimal value_of = BigDecimal.valueOf(l_val);

        System.out.println("l_val: " + l_val);
        System.out.println("valueOf(long): ");

        // Display value_of
        System.out.println("BigDecimal.valueOf(l_val): " + value_of);

        System.out.println();

        // converts the given double value into
        // a BigDecimal and store it in a variable
        // named value_of
        value_of = BigDecimal.valueOf(d_val);

        System.out.println("d_val: " + d_val);
        System.out.println("valueOf(double): ");

        // Display value_of
        System.out.println("BigDecimal.valueOf(d_val): " + value_of);

        System.out.println();

        // converts the given unscaled long value
        // with the given scale into a BigDecimal and
        // store it in a variable named value_of
        value_of = BigDecimal.valueOf(unscale_val, 5);

        System.out.println("unscale_val: " + unscale_val);
        System.out.println("valueOf(long,int): ");

        // Display value_of
        System.out.println("BigDecimal.valueOf(unscale_val,5): " + value_of);
    }
}

Output

l_val: 125487312456
valueOf(long): 
BigDecimal.valueOf(l_val): 125487312456

d_val: 1245871.12345
valueOf(double): 
BigDecimal.valueOf(d_val): 1245871.12345

unscale_val: 123458745123
valueOf(long,int): 
BigDecimal.valueOf(unscale_val,5): 1234587.45123


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.