问题
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