2011-09-01 73 views
-1

可能重複:
Is there a function to round a float in C or do I need to write my own?如何在用C語言打印時舍入類型浮點變量?

之前我造成任何混亂......這是我使用什麼C++

cout.setf(ios::fixed); 
.. 
cout.precision(3); 
.. 

我用圓所有打印數字在C++中保留3位小數。我無法在C中工作。任何幫助?

+3

http://stackoverflow.co m/questions/497018/is-there-a-function-to-round-a-float-in-c-or-do-i-need-to-write-my-own這是重複的。請關閉。 – mydogisbox

回答

0

可以實現與printf的:

printf("%0.3f", n); 
+0

非常感謝你 – Alex

0

試試這個:

printf("%.3f", float_value); 

其中.3f表示要精確3.

+0

你做錯了。你的代碼示例暗示'sprintf'不是'printf'。 – jer

+0

謝謝。這工作 – Alex

1

這樣做:

printf("%0.3f", myFloat);