使用sprintf打印浮点值 [英] Printing float values with sprintf
本文介绍了使用sprintf打印浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
printf('Rating:%.2F',$ rating);
打印如下:
4.00
例如:
例如:
/ p>- 4.00应该是4
- 4.20应该是4.2
- 4.02应该是4.02:)
解决方案
printf (评级:%g \ n,4.00);
printf(Rating:%g \\\
,4.20);
printf(Rating:%g \\\
,4.02);
打印
评分:4
评分:4.2
评分:4.02
所以将打印的值不 printf
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 :)
解决方案
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
这篇关于使用sprintf打印浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文