Monday, July 15, 2024
HomeLanguagesJavaBigDecimal toString() Method in Java with Examples

BigDecimal toString() Method in Java with Examples

The java.math.BigDecimal.toString() method is used to represent the current BigDecimal by which this method is called into String form, using scientific notation if an exponent is needed. It is done by following steps:

  • A standard canonical string form of the BigDecimal is created by converting the absolute value of the unscaled value of BigDecimal in base ten using the characters ‘0’ through ‘9’ with no leading zeros except when the value is 0, then single character ‘0’ is used.
  • Next, an adjusted exponent is calculated which is one less than adding the number of characters in the converted unscaled value and negated scale value. That is, -scale + (ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).
  • An exponent in character form is then suffixed to the converted unscaled value (perhaps with inserted decimal point). This comprises the letter ‘E’ followed immediately by the adjusted exponent converted to a character form.
  • Finally, the entire string is prefixed by a minus sign character ‘-‘ if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive.

Syntax:

public String toString()

Parameter: This method do not accepts any parameter.

Return value: This method returns the String representation of this BigDecimal number.

Overrides: This method overrides java.lang.Object.toString() method of Object class.

Below programs illustrates the use of toString() method in java

Example 1: Example to convert BigDecimal into String without Scientific notation




// Java program to demonstrate
// toString() 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"
                       + "3264736274673864"
                       + "7364732463546354"
                       + "6354632564532645"
                       + "6325463546536453"
                       + "6546325463546534"
                       + "6325465345326456"
                       + "4635463263453264"
                       + "654632498739473";
  
        // Converting to BigDecimal
        b = new BigDecimal(input);
  
        // Apply toString() method
        String s = b.toString();
  
        // Print the result
        System.out.println(s);
    }
}


Output:

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473

Example 2: Example to convert BigDecimal into String with Scientific notation




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


Output:

4.5368E+10

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

Last Updated :
05 Apr, 2019
Like Article
Save Article
Similar Reads
Related Tutorials
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