BigDecimal valueOf() Method in Java

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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_right

    
    

    Output:

    The Converted BigDecimal value is: 745812345678
    
  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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_right

    
    

    Output:

    The Converted BigDecimal value is: 785.254
    
  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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_right

    
    

    Output:

    The BigDecimal value is 789.654
    
  4. Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#valueOf(double)



    My Personal Notes arrow_drop_up

    Contented with little yet wishing for more

    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.