Java中printf的双%格式化问题 [英] Double % formatting question for printf in Java
本文介绍了Java中printf的双%格式化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
%s
是 printf
中的字符串,%d是我认为的小数......但是放入
%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);
..抛出异常告诉我%d
!= lang.double。想法?
..an exception is thrown telling me that %d
!= lang.double. Ideas?
推荐答案
%d
适用于整数使用%f
相反,它适用于 float
和 double
类型:
%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
这篇关于Java中printf的双%格式化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文