java.math.BigDecimal.valueOf(double val) Method



Description

The java.math.BigDecimal.valueOf(double val) translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.

Declaration

Following is the declaration for java.math.BigDecimal.valueOf() method.

public static BigDecimal valueOf(double val)

Parameters

val − double to convert to a BigDecimal.

Return Value

This method returns a BigDecimal whose value is equal to or approximately equal to the value of val.

Exception

NumberFormatException − If val is infinite or NaN.

Example

The following example shows the usage of math.BigDecimal.valueOf() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create a BigDecimal object
      BigDecimal bg;

      // create a Double Object
      Double d = new Double("123.45678");

      // assign the bigdecimal value of d to bg
      // static method is called using class name
      bg = BigDecimal.valueOf(d);

      String str = "The Double After Conversion to BigDecimal is " + bg;

      // print bg value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

The Double After Conversion to BigDecimal is 123.45678
java_math_bigdecimal.htm
Advertisements