是否可以仅使用 printf 语句对双精度数进行舍入?

发布于 2024-08-06 11:20:04 字数 982 浏览 4 评论 0原文

显然这只是代码的一小部分。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烂柯人 2024-08-13 11:20:04

您真的、真的不应该在浮点变量中存储“舍入”值。浮点不准确会破坏这一点 - 您的 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.

谈情不如逗狗 2024-08-13 11:20:04

%.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.

滴情不沾 2024-08-13 11:20:04
{

float x;
float rounded_x;

printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%f", &x);
rounded_x = ((int)(x * 100 + .5) / 100.0);

printf( "The number you have entered is %.2f\n", rounded_x);
return 0;

}

感谢所有试图提供帮助的人!我终于明白了

{

float x;
float rounded_x;

printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%f", &x);
rounded_x = ((int)(x * 100 + .5) / 100.0);

printf( "The number you have entered is %.2f\n", rounded_x);
return 0;

}

Thank you to everyone who tried to help! I finally got it

酸甜透明夹心 2024-08-13 11:20:04

printf 不会改变数字的值,只是改变它的显示方式。另一种选择是

#include <math.h>

// round  double x  to 2 decimal places
x = 0.01 * floor(x * 100.0 + 0.5);

printf won't change the value of the number, just how it is displayed. An alternative is

#include <math.h>

// round  double x  to 2 decimal places
x = 0.01 * floor(x * 100.0 + 0.5);
猥琐帝 2024-08-13 11:20:04

这是没有意义的

scanf("%5.2d", &x);

你不能有一个整数,小数点后有数字。如果 x 是平的那么奇怪的事情就会发生。如果它是一个整数,为什么你在 printf 中保留 2 个小数位。

你到底想做什么?

编辑:

double x;
printf( "Please enter a positive number that has a fractional part with three or more decimal places\n" );
scanf( "%lf", &x );
printf( "The number you have entered is %5.2f\n", x + 0.005 );

我很确定 printf 只会截断。所以你需要加上 0.005 来四舍五入。

This doesn't make sense

scanf("%5.2d", &x);

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:

double x;
printf( "Please enter a positive number that has a fractional part with three or more decimal places\n" );
scanf( "%lf", &x );
printf( "The number you have entered is %5.2f\n", x + 0.005 );

I'm pretty sure printf only truncates. So you will need to add 0.005 to round it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文