是否可以仅使用 printf 语句对双精度数进行舍入?
显然这只是代码的一小部分。
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%5.2d", &x);
printf("The number you have entered is %5.2d\n" ,x);
这会自动四舍五入我输入的数字吗?或者还有其他方法可以做到这一点吗?
编辑:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
我做到了这一点,但我考虑到有人所说的 printf 只是“改变”它的读出方式。所以这显然不是正确的方式。我应该实现 pow() 函数吗?这会以某种方式起作用吗?
Edit2:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
好的,我已经到了如果我输入一个数字,它会四舍五入到整数的地步。 35.21 将舍入为 35,35.51 将舍入为 36 等。等等。
我如何将 35.2178 舍入为 35.22,将 35.2135 舍入为 35.21。 我如何获得小数点的某些幂而不是整数进行四舍五入?
Obviously this is just a fraction of the code.
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%5.2d", &x);
printf("The number you have entered is %5.2d\n" ,x);
Would this automatically round the number I type in? Or is there another way to do this?
Edit:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
Iv done this, but Im taking into consideration what someone had said about printf just "changing" the way it reads out. So this is obviously not the right way. Should I implement maybe the pow() function? Will that work with this somehow?
Edit2:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
Okay, iv gotten to the point where if i imput a number it will round to a whole number. 35.21 will round to 35, and 35.51 will round to 36 et. etc.
How would I get 35.2178 to round to 35.22, and 35.2135 to round to 35.21.
How would I get the certain powers of the decimal to round instead of the whole number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您真的、真的不应该在浮点变量中存储“舍入”值。浮点不准确会破坏这一点 - 您的 5.10 可能会变成 5.099999999941892 ,仅仅是因为实现可能无法准确存储 5.10 。
或者,读取整数,将其乘以 100,然后将其转换为 int(这会将其四舍五入到零)。这将使您的计算保持准确。
You really, really should not store "rounded" values in floating point variables. Floating point inaccuracy will ruin this - your 5.10 might become 5.099999999941892 simply because the implementation might not be able to store 5.10 exactly.
As an alternative, read the whole number, multiply it with 100 and convert it to int (which will round it towards zero). That will keep your calculations accurate.
“
%.2f
”会将双精度数舍入为 2 位数字。双精度数不是整数,并且%d
和%f
不可互换。"
%.2f
" will round a double to 2 digits. A double is not an integer, and%d
and%f
are not interchangeable.感谢所有试图提供帮助的人!我终于明白了
Thank you to everyone who tried to help! I finally got it
printf
不会改变数字的值,只是改变它的显示方式。另一种选择是printf
won't change the value of the number, just how it is displayed. An alternative is这是没有意义的
你不能有一个整数,小数点后有数字。如果 x 是平的那么奇怪的事情就会发生。如果它是一个整数,为什么你在 printf 中保留 2 个小数位。
你到底想做什么?
编辑:
我很确定 printf 只会截断。所以你需要加上 0.005 来四舍五入。
This doesn't make sense
You can't have an integer with numbers after the decmal point. if x is a flat then weird things will happen. If its an integer why are you after 2 decimal places in the printf.
What exactly are you trying to do?
Edit:
I'm pretty sure printf only truncates. So you will need to add 0.005 to round it.