How to convert BigDecimal to/from String in Java examples
- Admin
- Mar 10, 2024
- 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
toString
Related Examples of 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
BigDecimal 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 the 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, returning the correct actual value.
In another way, 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 way with the toString method, and the second with the toEngineeringString method. The third way, use the simple toPlainString method.
Using toString() method
The toString()
method is used to convert BigDecimal
to String
. It is available in the java class that extends the java.lang.object
class.
It returns the string number 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 a simple way 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, The toPlainString()
, 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
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 a 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:
Exception in thread "main" java.lang.NumberFormatException
The fix is that BigDecimal will convert the string which contains numbers only. Try to avoid the String with non-numeric characters only.
Conclusion
To summarize, In this article, we have learned how to convert BigDecimal
to String
and String
to BigDecimal
in Java.