Java.math.BigDecimal.toEngineeringString() Method

Advertisements


Description

The java.math.BigDecimal.toEngineeringString() returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

Returns a string that represents the BigDecimal 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.

If exponential notation is used for zero values, a decimal point and one or two fractional zero digits are used so that the scale of the zero value is preserved.

Unlike the output of toString(), the output of this method is not guaranteed to recover the same [integer, scale] pair of this BigDecimal if the output string is converting back to a BigDecimal using the string constructor.

The result of this method meets the weaker constraint of always producing a numerically equal result from applying the string constructor to the method's output.

Declaration

Following is the declaration for java.math.BigDecimal.toEngineeringString() method

public String toEngineeringString()

Parameters

  • NA

Return Value

This method returns the string representation of this BigDecimal, using engineering notation if an exponent is needed.

Exception

  • NA

Example

The following example shows the usage of math.BigDecimal.toEngineeringString() method

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

     public static void main(String[] args) {

        // create a BigDecimal object
        BigDecimal bg;

        // create a String object
        String s;

        bg = new BigDecimal("1E+4");

        // assign the engineering string value of bg to s
        s = bg.toEngineeringString();

	String str = "Engineering string value of " + bg + " is " + s;

        // print s value
        System.out.println( str );
    }
}

Let us compile and run the above program, this will produce the following result:

Engineering string value of 1E+4 is 10E+3



Advertisements
Advertisements