Is String.format %d always compatible with BigInteger?
Asked Answered
A

1

9

With String.format %d and its variants, we can easily format primitive integers and longs. However, does these formatting options work perfectly with BigInteger?

I've made some tests, and they seem to work well, for example:

public class Test6 {
    public static void main(final String args[]) {
        final java.math.BigInteger b = java.math.BigInteger.TEN.pow(999999);
        System.out.println(String.format("%,d", b));
    }
}

But are there any gotchas to watch out for?

Aciculate answered 14/8, 2014 at 3:18 Comment(0)
J
9

There is an extensive section in the documentation for format strings.

I suppose the only tricky part could be locale-sensitivity (like what decimal point to use), but that applies for the other numeric types, too.

The following conversions may be applied to BigInteger.

'd'

Requires the output to be formatted as a decimal integer. The localization algorithm is applied.

If the '#' flag is given FormatFlagsConversionMismatchException will be thrown.

Jolandajolanta answered 14/8, 2014 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.