Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

BigDecimal.toPlainString(): decimal text without an exponent

Use `toPlainString()` to write a `BigDecimal` without exponent notation. Parsing the resulting text restores the numeric value, but not always the original scale.

Version: Java SE 21

Syntax

public String toPlainString()

BigDecimal.toPlainString() is an instance method of java.math.BigDecimal. It has no overloads: call it with no arguments, as in value.toPlainString(). It returns a String without an exponent part (E), without rounding or changing the object.

With a positive scale, the number of digits after the decimal point matches the scale, so 1.2300 keeps its trailing zeros. With zero or negative scale, the result has no decimal point: 1E+3 becomes 1000. The output is locale-independent: it uses a decimal point and no digit grouping.

Use this method when you need text without an exponent. Parsing it with new BigDecimal(text) restores the same numeric value, but a negative scale becomes 0. That is why compareTo() returns 0 even though equals() can return false: equals() also checks scale. If you need to recover both the original value and scale, use toString(), which may use exponent notation.

Examples

import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal original = new BigDecimal("1E+3");
        String plain = original.toPlainString();
        BigDecimal parsed = new BigDecimal(plain);

        System.out.println(original.toString());
        System.out.println(plain);
        System.out.println(new BigDecimal("1.2300").toPlainString());
        System.out.println(new BigDecimal("-1E-7").toPlainString());
        System.out.println(original.scale() + " -> " + parsed.scale());
        System.out.println(original.compareTo(parsed));
        System.out.println(original.equals(parsed));
    }
}

Result

1E+3
1000
1.2300
-0.0000001
-3 -> 0
0
false

The first two lines contrast `toString()` with `toPlainString()`. The next two show trailing zeros and a small negative number. Parsing `1000` changes the scale from `-3` to `0`: the numeric comparison returns `0`, while `equals()` returns `false`.

Related lessons

  • Java · Lesson 4 · Basic · Version: 2
  • Java · Lesson 6 · Basic · Version: 2
  • Java · Lesson 69 · Basic · Version: 2

Sources