Java BigDecimal toString() Method with Example

BigDecimal Class toString() method: Here, we are going to learn about the toString() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 08, 2020

BigDecimal Class toString() method

  • toString() method is available in java.math package.
  • toString() method is used to represent string denotation of this BigDecimal with the help of scientific notation when an exponent field is required to denote BigDecimal as String.
  • toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toString() method does not throw an exception at the time of string representation.

Syntax:

    public String toString();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is String, it returns string denotation of this BigDecimal.

Example:

// Java program to demonstrate the example 
// of String toString() method of BigDecimal

import java.math.*;

public class ToStringOfBD {
    public static void main(String args[]) {
        // Initializes a variables -  
        // String type
        String str = "12642E5";

        // Initializes a BigDecimal object  
        BigDecimal b_dec = new BigDecimal(str);

        // represents this BigDecimal b_dec value as
        // a String by using the scientific notation
        String str_conv = b_dec.toString();
        System.out.println("b_dec.toString(): " + str_conv);
    }
}

Output

b_dec.toString(): 1.2642E+9


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.