开发者

Float display 0 number after 1.5

开发者 https://www.devze.com 2023-04-11 12:11 出处:网络
I have a float that adds numbers and its not displaying all the decimal points i wish for example i have 1.25 add 1.25 as a float and it shows as 1.5. How can i have it display as 1.50 fo开发者_开发问

I have a float that adds numbers and its not displaying all the decimal points i wish for example i have 1.25 add 1.25 as a float and it shows as 1.5. How can i have it display as 1.50 fo开发者_开发问答r example?


#include <cstdio>
/* ... */
printf("%.02f", f);

See the documentation for printf().


In C++, you can do it like this:

cout << setprecision(2) << f << endl;

EDIT:

This answer isn't completely correct. See comments.

This sets the total precision to 2 digits. (Not digits after the decimal place.)


Adding to @Mystical solution, you can try fixed formatting:

cout << setprecision(2) << fixed << f << endl;

Hope this helps!


Try using %.2f as a format string.

0

精彩评论

暂无评论...
验证码 换一张
取 消