Java BigDecimal toEngineeringString() Method with Example

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

BigDecimal Class toEngineeringString() method

  • toEngineeringString() method is available in java.math package.
  • toEngineeringString() method is used to represent string denotation of this BigDecimal with the help of engineering notation when an exponent is required to denote BigDecimal as a String.
  • toEngineeringString() 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.
  • toEngineeringString() method does not throw an exception at the time of representing this BigDecimal as a String.

Syntax:

    public String toEngineeringString();

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 object with the help of engineering notation.

Example:

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

import java.math.*;

public class ToEngineeringStringOfBD {
    public static void main(String args[]) {
        // Initializes a variable -  
        // String type
        String str = "12E+5";

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

        // By using toEngineeringString() method is 
        // used to represent this BigDecimal b_dec as
        // a String by using the engineering notation
        String str_conv = b_dec.toEngineeringString();
        System.out.println("b_dec.toEngineeringString(): " + str_conv);
    }
}

Output

b_dec.toEngineeringString(): 1.2E+6


Comments and Discussions!

Load comments ↻





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