简体   繁体   中英

Output two digits after decimal separator

Using fprintf, what is the format-string to output two digits after a decimal separator?

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

This always outputs 3 instead of 3.00

How do I fill up the digits after decimal separator with zeros? I want 3.00 written in the output file.

请改用%.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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