Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is String.format %d always compatible with BigInteger?

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?

like image 734
Lecky. not lackey Avatar asked Aug 14 '14 03:08

Lecky. not lackey


People also ask

Can you use %d Java?

%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.

What is %s in string format?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.


1 Answers

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.

like image 181
Thilo Avatar answered Oct 27 '22 15:10

Thilo