{

How to convert BigDecimal to/from String in java examples


In this blog post, We are going to learn BigInteger conversion examples on

  • How to Convert String from BigDecimal
  • How to convert BigDecimal to String

Other posts on BigInteger class in java

You can also check my previous posts on the BigInteger class in java.

java.math.BigDecimal class in java

This is a data type that can be used in financial and ERP applications where the precision of a number is important. BigDecimal is used to store the rounding of the numbers after arithmetic operations. It is an immutable type.

BigDecimal is a class declared in the java.math package of java language.

How do you create a BigDecimal in java?

Objects can be created using the constructor with string or double parameters.

an example:

BigDecimal bigDecimal = new BigDecimal("147.87932");  
System.out.println(bigDecimal);  

BigDecimal bigDecimal1 = new BigDecimal(147.87932);  
System.out.println(bigDecimal1);  

Output:

147.87932  
147.8793200000000069849193096160888671875  

The first is to create an object using the constructor with a string value and returned the correct actual value.

The second is created using the constructor with a double value and 147.87932 is converted to the closest double value 147.8793200000000069849193096160888671875.

Hope you understand How the BigDecimal object is created in java.

How to convert BigDecimal to String with an example in java?

In Java, we can convert BigDecimal to String in multiple ways.
The first is with the toString method, and the second is the toEngineeringString method. Finally, simple toPlainString method

Using toString() method

toString() method is available in any java class that extends java.lang.object class. It returns number string’s as String output. Syntax:

BigDecimal.toString()

code:

MathContext m = new MathContext(3);  
BigDecimal bg = new BigDecimal("3617E+4", m);  
System.out.println(bg.toString());  
BigDecimal bd3 = new BigDecimal("567.12345678901234561243");  
System.out.println(bd3.toString());  

output:

3.62E+7  
567.12345678901234561243  

Using toEngineeringString() method

This is also simple to convert with this approach.

The function toEngineeringString() returns the String value of a Bigdecimal object that does not have an exponent field. However, when compared to the function toString() method, this approach does not return the correct value in terms of scale value.

Syntax:

public String toEngineeringString()

Here is a complete example

MathContext m = new MathContext(3);  
BigDecimal bg = new BigDecimal("3617E+4", m);  
System.out.println(bg.toEngineeringString());  
BigDecimal bd3 = new BigDecimal("567.12345678901234561243");  
System.out.println(bd3.toEngineeringString());  

output:

36.2E+6  
567.12345678901234561243  

Using toPlainString() method

Third, there’s toPlainString(), which returns a plain String of a bigdecimal object without the exponent field.

Syntax:

public String toPlainString()
MathContext m = new MathContext(3);  
BigDecimal bg = new BigDecimal("3617E+4", m);  
System.out.println(bg.toPlainString());  
BigDecimal bd3 = new BigDecimal("567.12345678901234561243");  
System.out.println(bd3.toPlainString());  

output:

36200000  
567.12345678901234561243  

String valueOf() method

It is an easy method and The String.valueOf() function in Java returns a string representation of any class.

Syntax:

String.valueOf(AnyObject)

Input parameter:

  • Any object in class, like BigDecimal Returns:
  • string version of a provided object, in this case, BigDecimal Object

Example:

MathContext mc = new MathContext(3);  
BigDecimal bd = new BigDecimal("1237E+4", mc);  
System.out.println(String.valueOf(bd));  
BigDecimal bd1 = new BigDecimal("123.12345678901234561243");  
System.out.println(String.valueOf(bd1));  

Output:

1.24E+7  
123.12345678901234561243  

How to Convert String to BigDecimal in Java?

It is very easy to do the conversion to BigDecimal from a given string, The simple approach is using the BigDecimal constructor

Syntax:

BigDecimal(String)

This constructor accepts string value, where the string is a number enclosed in double-quotes.

If the given number is not a number enclosed as a string, it gives NumberFormatException runtime error.

String stringObject="123.45";  
BigDecimal bigDecimalObject=new BigDecimal(stringObject);  
System.out.println(bigDecimalObject);  

Output:

123.45  

NumberFormatException error This issue comes when the string contains a non-numeric character

String stringObject="123.45";  
BigDecimal bigDecimalObject=new BigDecimal(stringObject);  
System.out.println(bigDecimalObject);  

and output is

Exception in thread "main" java.lang.NumberFormatException  

Conclusion

The fix is to BigDecimal will convert the string which contains numbers only. Try to avoid the String with non-numeric characters only.

Hope you understand the conversion of string and biginteger in java. Please like or leave a comment on Facebook/Twitter and subscribe to email for our latest feeds.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.