Java toString() method :

If you want to represent any object as a string, toString() method comes into existence.The toString() method returns the string representation of the object.

Example :

Student s1 = new Student(101,"Raj","lucknow");  
Student s2 = new Student(102,"Vijay","ghaziabad");  

System.out.println(s1);//compiler writes here s1.toString()  
System.out.println(s2);//compiler writes here s2.toString()  

//Output : 101 Raj lucknow
           102 Vijay ghaziabad

Java toPlainString() method :

The java.math.BigDecimal.toPlainString() returns a string representation of this BigDecimal without an exponent field.

Example :

MathContext mc = new MathContext(3); // 3 precision
BigDecimal bigDecimal = new BigDecimal("1234E+4", mc);
// Assign the plain string value of bigDecimal to s
String plainString = bigDecimal.toPlainString();

String str = "Plain string value of " + bigDecimal + " is " + plainString;

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

//Output : Plain string value of 1.23E+7 is 12300000
Answer from Shiladittya Chakraborty on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › java bigdecimal toplainstring method
Java BigDecimal toPlainString Method
September 1, 2008 - The java.math.BigDecimal.toPlainString() returns a string representation of this BigDecimal without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale.
🌐
Quora
quora.com › What-is-the-difference-between-toPlainString-and-toString-in-java
What is the difference between toPlainString() and toString() in java? - Quora
Answer (1 of 3): As per java language reference : public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a per...
🌐
Jaspersoft Community
community.jaspersoft.com › knowledge base › how-to
BigDecimal toPlainString() method - How-To - Jaspersoft Community
November 1, 2011 - The toString() method may use scientific notation while toPlainString() never will. If you find your self constantly having to deal with very large or very small numbers in the BigDecimal object, but do not wish to display it in a scientific ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - There is a one-to-one mapping between the distinguishable BigDecimal values and the result of this conversion. That is, every distinguishable BigDecimal value (unscaled value and scale) has a unique string representation as a result of using toString.
🌐
Robert Notes
robertnotes.com › 2024 › 11 › 06 › big-decimal-to-string.html
Things about calling toString on BigDecimal | Robert Notes
November 6, 2024 - val numbers = listOf( BigDecimal("12345.56789"), // Regular number BigDecimal("1234567890.0123456789"), // Regular number with high precision BigDecimal("1E20"), // Very large number BigDecimal("1E-8"), // Very small number BigDecimal("999999999999999999.999999999999999999") // Large precision ) val iterations = 1_000_000 numbers.forEach { number -> println("\nTesting with number: $number") val toStringTime = measureNanoTime { repeat(iterations) { number.toString() } } val toPlainStringTime = measureNanoTime { repeat(iterations) { number.toPlainString() } } println("toString() took: ${toStringTime / iterations} ns per call") println("toPlainString() took: ${toPlainStringTime / iterations} ns per call") println("toPlainString() is ${toPlainStringTime.toFloat() / toStringTime.toFloat()}x slower") }
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-toplainstring-method-in-java-with-examples
BigDecimal toPlainString() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - 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 ...
🌐
GitHub
github.com › spring-projects › spring-data-couchbase › issues › 1468
serialize BigDecimal with toString() instead of toPlainString() for compatibility with non- spring-data couchbase. · Issue #1468 · spring-projects/spring-data-couchbase
June 17, 2022 - Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access. - serialize BigDecimal with toString() instead of toPlainString() for compatibility with non- spring-data couchbase.
Published   Jun 17, 2022
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.toPlainString java code examples | Tabnine
public static String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return "0K"; } double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "KB"; } double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "MB"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigD
Find elsewhere
🌐
RoseIndia
roseindia.net › java › java-bigdecimal › toplain-string.shtml
Java bigdecimal toPlainString example
Method generates NumberFormatException, if it finds the bigdecimal value other than integers and double types values. Syntax for using the method: public String toPlainString() BigDecimal obj = new BigDecimal(integer); String str = obj.toPlainString(); System.out.println(str);
🌐
Coderanch
coderanch.com › t › 559104 › java › Big-Decimal-String
Big Decimal to String (Beginning Java forum at Coderanch)
As others have asked: Why? One other point. If and when you do upgrade, you will also have to change the output command, because the equivalent of BigDecimal.toString() in 1.4 is toPlainString() in 1.5 onwards - a very rare case of Java not being backwards-compatible.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
There is a one-to-one mapping between the distinguishable BigDecimal values and the result of this conversion. That is, every distinguishable BigDecimal value (unscaled value and scale) has a unique string representation as a result of using toString.
🌐
eHowToNow
ehowtonow.com › home › java › java bigdecimal toplainstring() method with example
Java BigDecimal toPlainString() method with example - eHowToNow - Java
March 13, 2021 - In particular, if this BigDecimal has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the toString method in 1.4 and earlier releases.) ... package com.ehowtonow.java.math.bigdecimal; public class ToPlainStringMethodExample { public static void main(String[] args) { } }
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_toengineeringstring.htm
Java.math.BigDecimal.toEngineeringString() Method
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-tostring-method-in-java-with-examples
BigDecimal toString() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - 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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 17 & JDK 17)
January 20, 2026 - The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-toengineeringstring-method-in-java-with-examples
BigDecimal toEngineeringString() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - 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.
Top answer
1 of 2
5

The reason why BigDecimal#PlainString takes very long to generate the string with Java 7 is: It was implemented very inefficiently in Java 7. Fortunately, it is much faster in Java 8.

Here, it may be important to note that in this particular case, it is not really the string creation in BigDecimal, but that in BigInteger. The value that is computed in the given example is a large factorial, and thus, effectively an integral value. The internal scale field of the BigDecimal will be 0 then, and having a look at the toPlainString method shows that in this case, the string value of the internal intVal field will be returned:

public String toPlainString() {
    if(scale==0) {
        if(intCompact!=INFLATED) {
            return Long.toString(intCompact);
        } else {
            return intVal.toString();
        }
    }
    ...
}

This intVal field is a BigInteger, and this is the actual culprit here.

The following program is not intended as a proper "microbenchmark", but only supposed to give an estimate of the performance: It creates several factorials, and generates the string representations of these:

import java.math.BigDecimal;

public class BigDecimalToPlainStringPerformance
{
    public static void main(String[] args)
    {
        for (int n = 10000; n <= 50000; n += 5000)
        {
            BigDecimal number = factorial(n);
            long before = System.nanoTime();
            String result = number.toPlainString();
            long after = System.nanoTime();

            double ms = (after - before) / 1e6;
            System.out.println(n + "! took " + ms +
                " ms, length " + result.length());
        }
    }

    private static BigDecimal factorial(int n)
    {
        BigDecimal number = new BigDecimal(1);
        for (int i = 1; i < n; i++)
        {
            number = number.multiply(new BigDecimal(i));
        }
        return number;
    }

}

With Java 7 (u07), on my (old) PC, the output is along the lines of

10000! took 514.98249 ms, length 35656
15000! took 1232.86507 ms, length 56126
20000! took 2364.799995 ms, length 77333
25000! took 3877.565724 ms, length 99090
30000! took 5814.925361 ms, length 121283
35000! took 8231.13608 ms, length 143841
40000! took 11088.823021 ms, length 166709
45000! took 14344.778177 ms, length 189850
50000! took 18155.089823 ms, length 213232

Fortunately, this performance problem has been fixed in Java 8. With Java 8 (u45), the output is

10000! took 77.20227 ms, length 35656
15000! took 113.811951 ms, length 56126
20000! took 188.293764 ms, length 77333
25000! took 261.328745 ms, length 99090
30000! took 355.001264 ms, length 121283
35000! took 481.912925 ms, length 143841
40000! took 610.812827 ms, length 166709
45000! took 698.80725 ms, length 189850
50000! took 840.87391 ms, length 213232

showing that the performance has been improved significantly.

From quickly skimming over the commit logs in the OpenJDK, there is one commit that is probably the most relevant here:

Accelerate conversion to string by means of Schoenhage recursive base conversion

(I did not verify this, but it seems the only one which dedicatedly aimed at improving the toString performance)

2 of 2
2

First off, your benchmark is not reproducible. In your case, the factorial part took 181324ms and the string generation took 710498ms, so string generation was 710498 / 181324 = 3.9 times as slow as the factorial part.

When I run it one time just like you wrote it, it gives these results.

Generating took: 90664ms. Creating String.
String generation took: 3465ms

So the string generation is 90644 / 3465 = 26 times as fast as the factorial part.

When you run a benchmark, you need to run it many times to take the average. Especially when you do a long-running microbenchmark like yours, because so many other things may be going on in your computer at the same time - perhaps your virus checker kicked in, or your Java process got swapped out to disk due to low memory, or Java decided to do a full garbage collection.

Secondly, you're not warming up the VM, so it's unclear what you are benchmarking. Are you benchmarking the HotSpot compilation engine's native compiler or your actual code? That's why you always need to warm up the VM before running microbenchmarks like yours.

The best way to do that is to use a proper microbenchmarking framework. An alternative is to run your code more times (using a for-loop) and when it settles down on timings that don't decrease anymore, you have a good indication that warm up has completed and you can take the average of the next couple of runs to come up with a number.

Running it on my MacBookPro, this resulted in an average of 80144 ms for the factorial part, and 2839 ms for the string generation (note I didn't look into memory usage yet for this).

So the string generation is 80144 / 2839 = 28 times as fast as the factorial part.

If you can reproduce the same result multiple times on your machine, while you are not touching it at all when you run your program, and your machine has enough memory, then there is something interesting going on. But the problem is not in the toPlainString() method in BigDecimal - that method is much faster than the factorial part of your code.