Convert BigDecimal to String in Java [2 ways]

In this post, I will be sharing how to convert BigDecimal to String in Java with examples. The different ways to achieve our goal are given below:

1. Using String.valueOf() method [Recommended]

2. Using toString() method

Read Also: Convert String to BigDecimal in Java

Convert BigDecimal to String in Java

1. Using String.valueOf() method

You can also use the String class valueOf() method to convert BigDecimal to String in Java. Internally, the String class valueOf() method is using the toString() method.

Note: The String class valueOf() method can deal with the null values whereas the toString() method throws NullPointerException.

 import java.math.BigDecimal;

public class BigDecimalToString {
    public static void main(String args[]) {
      // Creating BigDecimal Object Using Constructor
      BigDecimal bd = new BigDecimal("2345.6789");
      // Converting BigDecimal to String using valueOf()
      String strObj = String.valueOf(bd);
      // Displaying String
      System.out.println("Converted BigDecimal to String: " + strObj);
    }
}


Output:
Converted BigDecimal to String: 2345.6789


2. Using toString() method

You can easily use the BigDecimal class toString() method to convert BigDecimal to String in Java as shown in the below example.

 import java.math.BigDecimal;

public class BigDecimalToString2 {
    public static void main(String args[]) {
      // Creating BigDecimal Object
      BigDecimal bd = new BigDecimal("12345.67");
      // Converting BigDecimal to String using toString()
      String str = bd.toString();
      // Printing String
      System.out.println("Converted BigDecimal to String: " + str);
    }
}


Output:
Converted BigDecimal to String: 12345.67


That's all for today. Please mention in the comments if you have any questions related to how to convert BigDecimal to String in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry