Java.math.BigDecimal.longValue() Method

Advertisements


Description

The java.math.BigDecimal.longValue() converts this BigDecimal to an long.This conversion is analogous to the narrowing primitive conversion from double to short. Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an long, only the low-order 64 bits are returned.

This conversion can lose information about the overall magnitude and precision of this BigDecimal value as well as return a result with the opposite sign.

Declaration

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

public long longValue()

Specified by

  • longValue in class Number

Parameters

  • NA

Return Value

This method returns the long value of the BigDecimal Object

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

    public static void main(String[] args) {

        // create 2 BigDecimal objects
        BigDecimal bg1, bg2;

        // create 2 long objecs
        long l1,l2;

        bg1 = new BigDecimal("429.07");
        bg2 = new BigDecimal("429496732223453626252");

        // assign the long value of bg1 and bg2 to l1,l2 respectively
        l1 = bg1.longValue();
        l2 = bg2.longValue();

        String str1 = "long value of " + bg1 + " is " + l1;
	String str2 = "long value of " + bg2 + " is " + l2;

        // print l1,l2 values
        System.out.println( str1 );
        System.out.println( str2 );
    }
}

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

long value of 429.07 is 429
long value of 429496732223453626252 is 5221618528133939084



Advertisements
Advertisements