gpt4 book ai didi

c - 将数字打印到可变的小数位

转载 作者:太空狗 更新时间:2023-10-29 15:19:34 27 4
gpt4 key购买 nike

我想在 C 中打印一个小数位数可变的数字。我写了代码

#include<stdio.h>
main()
{ int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.af", a, 2.554648);
return 0;
}

最佳答案

printf()中使用*标记您想要的小数位数:

#include <stdio.h>

int main(void)
{
int a;

printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.*f", a, a, 2.554648);

return 0;
}

关于c - 将数字打印到可变的小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844907/

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