2014-01-25 50 views

回答

8

使用格式說明%.2f 2位精度。同樣使用%.3f 3位數字。爲了將來的參考,這裏是printfformat specifiers

#include <stdio.h> 

int main() 
{ 
    printf("%.2f\n", 0.005); // prints 0.01 
    printf("%.2f\n", 0.004); // prints 0.00 
} 
+1

審計@禁衛軍的回答,您還可以使用'%* F'例如'int精度= 3。的printf( 「%* F \ n」,精密,0.00455);'它輸出:'0.005' –

+0

@GrijeshChauhan我從來不知道有'* F',謝謝。 :) – herohuyongtao

+1

@herohuyongtao的''可在現場寬度說明使用也,並在這種情況下,參數被允許爲負太,其表示輸出應左對齊。 – Praetorian