Double % formatting question for printf in Java

前端 未结 4 1343
死守一世寂寞
死守一世寂寞 2020-12-14 15:46

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


        
4条回答
  •  甜味超标
    2020-12-14 16:41

    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.

提交回复
热议问题