Java.math.BigDecimal.valueOf() Method

Advertisements


Description

The java.math.BigDecimal.valueOf(long unscaledVal, int scale) translates 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.

Declaration

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

public static BigDecimal valueOf(long unscaledVal, int scale)

Parameters

  • unscaledVal - unscaled value of the BigDecimal

  • scale - scale of the BigDecimal

Return Value

This method returns a BigDecimal whose value is (unscaledVal × 10-scale)

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
	// scale is 4
        bg = BigDecimal.valueOf(l, 4);

	String str = "The Value of BigDecimal using scale 4 is " + bg;

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

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

The Value of BigDecimal using scale 4 is 1234.5678



Advertisements
Advertisements