Java String Formatting Java
This example shows how to format signed integers with String#printf()
String#printf()
Signed integer means it can hold both positive and negative values.
The format specifiers %+d is used for this purpose.
The specified signed is used as default which is overridden by the value sign if specified
Types include: byte, Byte, short, Short, int and Integer, long, Long, and BigInteger
The include parentheses to represent negative integers, the specifiers %(d is used.
package com.logicbig.example.string;public class StringPrintfSignedInteger { public static void main(String[] args) { //including + sign System.out.printf("%+d%n", 45); System.out.printf("%+d%n", -45); //including parentheses for negative integers System.out.printf("%(d%n", -25); System.out.printf("%(d%n", 25); }}
+45-45(25)25