Printing float values with sprintf

时光总嘲笑我的痴心妄想 提交于 2019-12-10 05:29:39

问题


Right now I have:

printf('Rating: %.2F', $rating);

which prints like:

4.00

How can I show the leading zero, only if there is something to show after it?

For example:

  • 4.00 should be 4
  • 4.20 should be 4.2
  • 4.02 should be 4.02 :)

回答1:


printf("Rating: %g\n", 4.00);
printf("Rating: %g\n", 4.20);
printf("Rating: %g\n", 4.02);

prints

Rating: 4
Rating: 4.2
Rating: 4.02

So will printing the values without printf

demo



来源:https://stackoverflow.com/questions/8204963/printing-float-values-with-sprintf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!