BigDecimal toPlainString() Method in Java with Examples

The java.math.BigDecimal.toPlainString() method is used to represent the current BigDecimal by which this method is called into String form without an exponent field. If the values are with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result.
Note: If the result of this method is passed to the string constructor, only the numerical value of this BigDecimal will be recovered, the representation of the new BigDecimal may have a different scale. In particular, this method behaves analogously to the toString method.

Syntax:

public String toPlainString()

Parameter: This method do not accepts any parameter.

Return value: This method returns the String representation of this BigDecimal number without an exponent field.

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

Example 1: Example to convert BigDecimal into plain String without exponent notation

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// toPlainString() 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 toPlainString() method
        String s = b.toPlainString();
  
        // Print the result
        System.out.println(s);
    }
}

chevron_right


Output:

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// toPlainString() 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;
  
        a = new BigDecimal("4536785E10");
  
        // apply toPlainString() method
        s = a.toPlainString();
  
        // print the result
        System.out.println(s);
    }
}

chevron_right


Output:

45367850000000000

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.