Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double % formatting question for printf in Java

Tags:

java

printf

%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?

like image 229
D. Spigle Avatar asked Oct 04 '10 06:10

D. Spigle


1 Answers

%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 
like image 135
codaddict Avatar answered Sep 16 '22 14:09

codaddict