How To Convert String To Bigdecimal In Java
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to convert string to bigdecimal in java, a BigDecimal object provides arithmetic, scale manipulations, rounding, comparisons, hashes, and format conversions. A BigDecimal can be represented as a string using the toString() method.
Here, we create a StringConverter<> generic interface with two generic methods.
You can use this interface to convert Wrapper classes (Integer, Short, Double, etc) to Strings 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.
We will now discuss the idea of how to convert string to bigdecimal in java with an example.
Step By Step Guide On How To Convert String To Bigdecimal In Java :-
package net.javaguides.string.conversion; import java.math.BigDecimal; public class TalkersCodeBigDecimalStringConverter<BigDecimal> { @Override public BigDecimal fromString(String value) { 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 (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); } }
- We import the BigDecimal class from the java.math package.
- We create a class called "TalkersCodeBigDecimalStringConverter" which implements the generic interface "StringConverter" from the JavaFX library. The generic type of this class is "BigDecimal", as we want to convert strings to and from BigDecimal objects.
- We override the "fromString" method, which takes a string value as input and converts it to a BigDecimal object. If the input value is null or zero-length, we return null. Otherwise, we trim the string value and create a new BigDecimal object from it, which we return.
- We override the "toString" method, which takes a BigDecimal object as input and converts it to a string value. If the input value is null, we return a zero-length string. Otherwise, we convert the BigDecimal object to a string using the toString() method and return it.
- In the main method, we create an instance of the TalkersCodeBigDecimalStringConverter class and use it to convert a string value "10.10" to a BigDecimal object. We then print the converted value using the System.out.println method.
- Next, we convert the BigDecimal object back to a string value using the toString method and print it using the System.out.println method.
Conclusion :-
As a result, we have successfully learned how to convert string to bigdecimal in java with an example.
Using BigDecimal, a string containing only numbers will be converted. Don't use Strings that contain only non-numeric characters.
Hopefully you have understood how String and BigInteger are converted in Java.
I hope this article on how to convert string to bigdecimal in java helps you and the steps and method mentioned above are easy to follow and implement.