Java Convert BigDecimal to String

Tags: BigDecimal String DecimalFormat

In this Java core tutorial we learn how to convert a BigDecimal value into a String value with different simple solutions in Java programming language.

Table of contents

  1. Convert BigDecimal to String using toString() method
  2. Convert BigDecimal to String using String.valueOf() method
  3. Format BigDecimal value using DecimalFormat class

Convert BigDecimal to String using toString() method

The first solution, we can use BigDecimalValue.toString() method get String value of a BigDecimal object as following Java program.

BigDecimalToStringExample1.java

import java.math.BigDecimal;

public class BigDecimalToStringExample1 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue = new BigDecimal(9999999.55555);

        String stringValue = bigDecimalValue.toString();
        System.out.println(stringValue);
    }
}
The output as below.
9999999.55554999969899654388427734375

Convert BigDecimal to String using String.valueOf() method

The second solution, we can use String.valueOf() to instantiate a String value from a BigDecimal object as the below Java example.

BigDecimalToStringExample2.java

import java.math.BigDecimal;

public class BigDecimalToStringExample2 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue = new BigDecimal(54321.99);

        String stringValue = String.valueOf(bigDecimalValue);
        System.out.println(stringValue);
    }
}
The output as below.
54321.9899999999979627318680286407470703125

Format BigDecimal value using DecimalFormat class

With the third solution, we can use the java.text.DecimalFormat class to format a BigDecimal object to a String.

BigDecimalToStringExample3.java

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class BigDecimalToStringExample3 {
    public static void main(String... args) {
        BigDecimal bigDecimalValue = new BigDecimal(9999999.123);

        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMinimumFractionDigits(2);
        decimalFormat.setMaximumFractionDigits(10);
        decimalFormat.setGroupingUsed(false);

        String stringValue = decimalFormat.format(bigDecimalValue);

        System.out.println(stringValue);
    }
}
The output as below.
9999999.1229999997

Happy Coding 😊

Java Convert String to BigDecimal

Java Compare two BigDecimal values