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



Description

The java.math.BigDecimal.valueOf(long val) translates a long value into a BigDecimal with a scale of zero. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigDecimal values.

Declaration

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

public static BigDecimal valueOf(long val)

Parameters

val − Value of the BigDecimal.

Return Value

This method returns a BigDecimal whose value is val.

Exception

NA

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 Long Object
      Long l = new Long("12345678");

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

      String str = "The Long 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 Long After Conversion to BigDecimal is 12345678
java_math_bigdecimal.htm
Advertisements