Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%( convert negative number to positive in System.out.printf

Tags:

java

The output of this code

System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3);

is

3 (3) +3 00003

can somebody please explain why the -3 is printed as 3 in this statement?

like image 831
Kamran Ali Avatar asked Dec 01 '22 21:12

Kamran Ali


2 Answers

can somebody please explain why the -3 is printed as 3 in this statement?

Its isn't, it is being printed as (3)

From the Javadoc for Formatter say flag (

The result will enclose negative numbers in parentheses

like image 127
Peter Lawrey Avatar answered Dec 24 '22 01:12

Peter Lawrey


The Formatter help says that the format "(" means it will enclose negative numbers in parentheses.

In bookkeeping, amounts owed are often represented by red numbers, or a number in parentheses, as an alternative notation to represent negative numbers.

From Wikipedia: Negative Numbers

like image 25
Edwin Dalorzo Avatar answered Dec 24 '22 00:12

Edwin Dalorzo