×

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

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

How to add 6 Months to the Current Date in Java?

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

3 minutes read.

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is...

4 minutes read.