简体   繁体   English

小数点分隔符后输出两位数字

[英]Output two digits after decimal separator

Using fprintf, what is the format-string to output two digits after a decimal separator? 使用fprintf,在十进制分隔符后输出两位数字的格式字符串是什么?

float number = 3.0;
fprintf(pFile, "%10.02f", number); 

This always outputs 3 instead of 3.00 这总是输出3而不是3.00

How do I fill up the digits after decimal separator with zeros? 如何在小数点分隔符后的数字填充零? I want 3.00 written in the output file. 我想在输出文件中写入3.00。

请改用%.2f作为格式说明符,或者如果要先%10.2f 10个空格,请使用%10.2f

I suspect you want to represent currency amounts. 我怀疑您想代表货币金额。 Please don't use floats for this purpose. 请不要为此目的使用浮点数。 Decimal numbers aren't represented exactly with floats, this can lead to roundoff errors you don't want. 十进制数字不能完全用浮点数表示,这可能会导致舍入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM