BigDecimal toString() in Java
BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class has operations for adding, subtracting, multiplying, dividing, rounding, comparing, hashing, and changing formats. The math. Java. The current BigDecimal, from which this function is called, is converted into a String using following syntax if an exponent is required by BigDecimal. function toString() method.
For diverse reasons, the class offers a variety of methods. The function toString() of the BigDecimal class, which is often used, will be covered in this section (). A Java application should also be written to convert BigDecimal to String.
The BigDecimal class has operations for adding, subtracting, multiplying, dividing, rounding, comparing, hashing, and changing formats. A BigDecimal can be canonically represented using the function toString()function. The BigDecimal class provides total control over rounding behaviour to the user.
BigDecimal toString() Method
A canonical representation of the provided BigDecimal is offered by the function toString() of the BigDecimal class. It overrides the Object class's function toString(). And if an exponent is required, it returns the textual representation of this BigDecimal in scientific notation.
Syntax
public String toString()
There are no parameters accepted by the procedure. It gives back a BigDecimal representation as a string.
Program1 for BigDecimal toString() Method
BigDecimal1.java
// importing required packages
import java . math . BigDecimal ;
// Declaring public class with name BegDecimal1
public class BigDecimal1
{
// Main section of the program where execurion of the program begins
public static void main ( String args [ ] )
{
// Creating and initializing variable for BigDecimal
BigDecimal big_decimal = new BigDecimal ( 500.3224 ) ;
// Creating string variable
// Converting bigdecimal it into sstring
String string1 = big_decimal . toString ( ) ;
// Printing the string which is converted from bigdecimal
System . out . println ( string1 ) ;
}
}
Output

We passed a double value as a parameter in the preceding programme. We can see that we do not get the number that we gave as a parameter to the function Object() . The reason for this is that the double type cannot represent the actual amount, which is 500.3224. As a result, the value 500.3224 is transformed into the nearest possible double, which is 500.3224000000000160071067512035369873046875.It is one of the drawbacks of utilising the function toString() method.
Program1 for BigDecimal toString() Method
BigDecimal2.java
// importing required packages
import java . math . BigDecimal ;
// Declaring public class with name BegDecimal2
public class BigDecimal2
{
// Main section of the program where execurion of the program begins
public static void main ( String args [ ] )
{
// Creating and initializing variable for BigDecimal
BigDecimal big_decimal = new BigDecimal ( 3421.1 ) ;
// Creating string variable
// Converting bigdecimal it into sstring
String string1 = big_decimal . toString ( ) ;
// Printing the string which is converted from bigdecimal
System . out . println ( string1 ) ;
}
}
Output

Convert BigDecimal to String in Java
There are two ways to solve the aforementioned issue:
- Value being sent as a String
- Applying the function valueOf ( ) Method
Value being sent as a String
As previously discussed, if a double value is passed to the function Object(), a huge number that is near to the desired number is returned. We must supply the parameter as a string in order to obtain the precise number.
Program2 for BigDecimal toString() Method
BigDecimal2.java
// importing the required packages
import java . math . BigDecimal ;
// declaring the public main class
public class BigDecimal2
{
// Main section of the program where execution of the program starts
public static void main ( String args [ ] )
{
BigDecimal big_decimal = new BigDecimal ( " 500.3224 " ) ;
String string1 = big_decimal . toString ( ) ;
System . out . println ( string1 ) ;
}
}
Output

Applying the function valueOf ( ) Method
Based on the double's canonical representation, the procedure creates a BigDecimal. BigDecimal can be converted to String using this method. It employs the function toString() method internally. Therefore, it will be convenient to use the function toString() function for conversion.
Program3 for BigDecimal toString() Method
BigDecimal3.java
// Importing required packages for the program
import java.math.BigDecimal;
public classBigDecimal3
{
public static void main(String args[])
{
BigDecimal big_decimal = BigDecimal . valueOf ( 500.3224 ) ;
System . out . println ( big_decimal . toString ( ) ) ;
}
}
Output
