How to convert BigDecimal to/from String in java examples
- Admin
- Sep 25, 2023
- Java-convert Java
In this blog post, We are going to learn about BigInteger examples on
- Create BigInteger object using constructor, Example
new BigInteger(123)
- How to Convert
String
fromBigDecimal
- 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.
- BigInteger Class tutorials
- Convert BigInteger to/from BigDecimal
- BigInteger Divide example
- BigInteger Multiplication example
- Convert BigDecimal to float
- Convert BigDecimal to double
- Top 10 BigInteger Examples
- Rounding bigdecimal to 2 decimal places
- Check BigDecimal contains Zero or not
- Convert BigInteger to/from ByteArray
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:
// constructor with String parameter
BigDecimal bigDecimal = new BigDecimal("147.87932");
System.out.println(bigDecimal);
// constructor with Double parameter
BigDecimal bigDecimal1 = new BigDecimal(147.87932);
System.out.println(bigDecimal1);
Output:
147.87932
147.8793200000000069849193096160888671875
First example, create an object using the constructor with a string
value, returned the correct actual value.
The second, created using the constructor with a double
value, 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 a 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.