Sunday, August 12, 2018

How to convert BigDecimal to String or String to BigDecimal in java with examples

java.math.BigDecimal class

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

BigDecimal Example

Objects can be created using the constructor with string or double parameters. here is an example
BigDecimal bigDecimal = new BigDecimal("147.87932");
System.out.println(bigDecimal);
BigDecimal bigDecimal1 = new BigDecimal(147.87932);
System.out.println(bigDecimal1);
Output generated
147.87932
147.8793200000000069849193096160888671875
First is created an object using the constructor with a string value and returned correct actual value.
 second is created using the constructor with double value and 147.87932 is converted to closest double value 147.8793200000000069849193096160888671875.
 Hope you understand the creation of a bigdecimal object in different ways.

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

There are many ways we can convert to String in Java.

Using toString() method.

Every class which extends Object class has toString() method. this method returns the string form the number
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());
and output is
3.62E+7
567.12345678901234561243

Using toEngineeringString() method

This method returns String object of Bigdecimal without an exponent field
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());
and output is
36.2E+6
567.12345678901234561243

Using toPlainString() method

This returns plain String of bigdecimal object avoiding exponent field
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());
and output is
36200000
567.12345678901234561243

using valueOf() method 

The string provides a valueOf method which accepts BigDecimal Object
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));
and output is
1.24E+7
123.12345678901234561243

How to Convert String to BigDecimal in Java?

 It is very easy to do the conversion to BigDecimal BigDecimal constructor accepts string value where the string is number enclosed in double quotes
String stringObject="123.45";
BigDecimal bigDecimalObject=new BigDecimal(stringObject);
System.out.println(bigDecimalObject);
and output is

123.45
NumberFormatException error 
This issue comes when the string contains 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
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 in facebook/twitter and subscribe to email for our latest feeds.


EmoticonEmoticon

Note: Only a member of this blog may post a comment.