×

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

BigDecimal toString() in Java

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

BigDecimal toString() in Java

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

BigDecimal toString() in Java

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

BigDecimal toString() in Java

Related Topics

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.