我的程序目標是在用戶指定的給定點切斷pi。如何自動擺脫尾隨零?
我已經想出瞭如何做到這一點,但我不知道如何擺脫尾隨零。
#include <stdio.h>
#include <math.h>
int main()
{
int i,length;
double almost_pi,pi;
printf("How long do you want your pi: ");
scanf("%d", &length);
pi = M_PI;
i = pi * pow(10,length);
almost_pi = i/pow(10,length);
printf("lf\n",almost_pi);
return 0;
}
假設用戶輸入3,它應該返回3.141,但它返回3.141000。
請和謝謝!
http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders – 2013-03-02 04:14:20
此[文章] [1]將幫助您開始正確的方向。 [1]:http://stackoverflow.com/questions/7425030/how-can-i-limit-the-number-of-digits-displayed-by-printf-after-the-decimal-點 – jarmod 2013-03-02 04:18:17
3.141和3.141000是一樣的。對於這個問題,它也和3.141000000000000000一樣。您的問題是顯示值,而不是輸入的內容。 – 2013-03-02 04:35:48