Java Conversion between BigDecimal and String Tutorial

In this tutorial, we will learn how to convert BigDecimal and String and also we will convert String to BigDecimal back.
The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.
In this tutorial, we create StringConverter<> generic interface with two generic methods. This interface can be used to convert any wrapper classes (Integer, Short, Double, etc) to String and vice-versa.
For example - in this tutorial, we are converting BigDecimal and String so we will create BigDecimalStringConverter class which implements StringConverter<> interface methods similarly if you want to convert Double to String then create DoubleStringConverter class and implement StringConverter<> interface methods.

Create a StringConverter Generic Interface

Here we create a generic StringConverter<> interface. This interface declares the following generic methods. 
  • toString(T object) - Converts the object provided into its string form. 
  • T fromString(String string) - Converts the string provided into an object defined by the specific converter.
package net.javaguides.string.conversion;

/**
 * Converter defines conversion behavior between strings and objects.
 * The type of objects and formats of strings are defined by the subclasses
 * of Converter.
 */
public interface StringConverter<T> {
    /**
    * Converts the object provided into its string form.
    * @return a string representation of the object passed in.
    */
    public abstract String toString(T object);

    /**
    * Converts the string provided into an object defined by the specific converter.
    * @return an object representation of the string passed in.
    */
    public abstract T fromString(String string);
}

BigDecimalStringConverter.java

Let's create a BigDecimalStringConverter class which converts BigDecimal and String and String to BigDecimal back.
package net.javaguides.string.conversion;

import java.math.BigDecimal;

/**
 * Conversion between BigDecimal and String in Java
 * @author Ramesh Fadatare
 *
 */
public class BigDecimalStringConverter implements StringConverter<BigDecimal> {

      @Override
      public BigDecimal fromString(String value) {
          // If the specified value is null or zero-length, return null
          if (value == null) {
               return null;
          } 

          value = value.trim();

          if (value.length() < 1) {
              return null;
          }

          return new BigDecimal(value);
     }

     @Override
     public String toString(BigDecimal value) {
           // If the specified value is null, return a zero-length String
           if (value == null) {
               return "";
           }

           return ((BigDecimal) value).toString();
     }
 
     public static void main(String[] args) {
  
           String str = "10.10";
          BigDecimalStringConverter bigDecimalStringConverter = new BigDecimalStringConverter();
          BigDecimal decimal = bigDecimalStringConverter.fromString(str);
          System.out.println("convert string to decimal number -> " + decimal);
  
          String decimalStr = bigDecimalStringConverter.toString(decimal);
          System.out.println("convert decimal number to string -> " + decimalStr);  
     }
}
Output:
convert string to decimal number -> 10.10
convert decimal number to string -> 10.10
In this above example, we are converting String to BigDecimal with the following method:
@Override
public BigDecimal fromString(String value) {
     // If the specified value is null or zero-length, return null
      if (value == null) {
            return null;
      }

      value = value.trim();

      if (value.length() < 1) {
          return null;
     }

     return new BigDecimal(value);
}
In this above example, we are converting BigDecimal to String with the following method:
@Override
public String toString(BigDecimal value) {
     // If the specified value is null, return a zero-length String
     if (value == null) {
           return "";
     }

     return ((BigDecimal) value).toString();
}

Reference

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course