Wednesday, July 3, 2024
HomeLanguagesJavaBigDecimal toEngineeringString() Method in Java with Examples

BigDecimal toEngineeringString() Method in Java with Examples

The java.math.BigDecimal.toEngineeringString() method is used to represent the current BigDecimal by which this method is called into String form by using engineering notation if an exponent is needed. The string representation of the BigDecimal is the same as described in the toString() method, except that if exponential notation is used, the power of ten is adjusted to be a multiple of three (engineering notation) such that the integer part of nonzero values will be in the range 1 through 999.
Syntax:

public String toEngineeringString()

Parameter: This method do not accepts any parameter.
Return value: This method returns the Engineering String representation of this BigDecimal number.

Below programs illustrates the use of toEngineeringString() method in java
Example 1: Example to convert BigDecimal into Engineering String without exponent notation




// Java program to demonstrate
// toEngineeringString() method of BigDecimal
  
import java.math.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating a BigDecimal object
        BigDecimal b;
  
        // Object of String to hold the number
        String input = "012345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4554324324362432"
                       + "7674637264783264"
                       + "7832678463726478"
                       + "4635463263453264"
                       + "654632498739473";
  
        // Converting to BigDecimal
        b = new BigDecimal(input);
  
        // Apply toEngineeringString() method
        String s = b.toEngineeringString();
  
        // Print the result
        System.out.println(s);
    }
}


Output:

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264784635463263453264654632498739473

Example 2: Example to convert BigDecimal into Engineering String with exponent notation




// Java program to demonstrate
// toEngineeringString() method of BigDecimal
  
import java.math.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a BigDecimal object
        BigDecimal a = new BigDecimal("4536785E10");
  
        // Create a String object
        String s;
  
        // apply toEngineeringString() method
        s = a.toEngineeringString();
  
        // print the result
        System.out.println(s);
    }
}


Output:

45.36785E+15

References: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#toEngineeringString()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments