gpt4 book ai didi

c - 如何自动去除尾随零?

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:30 25 4
gpt4 key购买 nike

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

请和谢谢!

最佳答案

这就是你想要做的吗??

#include <stdio.h>
#include <math.h>

int main()
{
int length = 4;
printf("How long do you want your pi: ");
if (1 == scanf("%d", &length))
printf("%.*f\n", length, M_PI);
return 0;
}

示例输出 #1

How long do you want your pi: 4
3.1416

示例输出 #2

How long do you want your pi: 12
3.141592653590

关于c - 如何自动去除尾随零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com