%s
is a string in printf
, and %d is a decimal I thought...yet when putting in
writer.printf("%d dollars is the balance of %s\r\n", bal, nm);
..an exception is thrown telling me that %d
!= lang.double. Ideas?
%s
is a string in printf
, and %d is a decimal I thought...yet when putting in
writer.printf("%d dollars is the balance of %s\r\n", bal, nm);
..an exception is thrown telling me that %d
!= lang.double. Ideas?
%d
is for integers use %f
instead, it works for both float
and double
types:
double d = 1.2;
float f = 1.2f;
System.out.printf("%f %f",d,f); // prints 1.200000 1.200000
Yes, %d
means decimal, but it means decimal number system, not decimal point.
Further, as a complement to the former post, you can also control the number of decimal points to show. Try this,
System.out.printf("%.2f %.1f",d,f); // prints 1.20 1.2
For more please refer to the API docs.