Java Convert long to BigDecimal

Tags: BigDecimal long

In this Java core tutorial we learn how to convert a long value to a java.math.BigDecimal object in Java programming language.

How to convert long to BigDecimal in Java

In Java, with a given long value we can use the BigDecimal.valueOf(long val) static method to instantiate a new BigDecimal object from a long value as the following example Java code.

ConvertLongToBigDecimalExample1.java

import java.math.BigDecimal;

public class ConvertLongToBigDecimalExample1 {
    public static void main(String... args) {
        long longValue = 111222333;

        // Convert long value to BigDecimal object
        BigDecimal bigDecimalValue = BigDecimal.valueOf(longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("BigDecimal value: " + bigDecimalValue);
    }
}
The output as below.
Long value: 111222333
BigDecimal value: 111222333

We can also use the BigDecimal(long val) constructor to create a new instance of BigDecimal from a given long value as the Java code below.

ConvertLongToBigDecimalExample2.java

import java.math.BigDecimal;

public class ConvertLongToBigDecimalExample2 {
    public static void main(String... args) {
        long longValue = 900800700;

        // Convert long value to BigDecimal object
        BigDecimal bigDecimalValue = new BigDecimal(longValue);

        System.out.println("Long value: " + longValue);
        System.out.println("BigDecimal value: " + bigDecimalValue);
    }
}
The output as below.
Long value: 900800700
BigDecimal value: 900800700

Happy Coding 😊

Java Convert int to BigDecimal

Java Convert double to BigDecimal

Java Convert float to BigDecimal

Java Convert BigInteger to BigDecimal

How to Negate BigDecimal Value in Java

How to Round BigDecimal Value in Java

How to Get Absolute of BigDecimal Value in Java

Java Convert BigDecimal to BigInteger