我的程序目标是在用户指定的给定点切断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