将正数向上舍入两位

发布于 2024-10-17 03:57:06 字数 577 浏览 2 评论 0原文

大家好,我正在尝试编写一个程序,该程序将采用带小数部分的正数并将其四舍五入两位。例如,32.4851 将四舍五入为 32.49,32.4431 将四舍五入为 32.44。

我真的对这个有点迷失,希望你们能帮助我一点。我已经使用 stdio.h 和 math.h 编写了一些代码并需要反馈(不会使用 gcc 进行编译)

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

double x;

int rounded_x;

int main (void)
{
        printf ("Enter a number to be rounded \n\n\n\n\n");
        scanf ("&lf", &x);
        rounded_x=(int) (x+0.5);
        return 0;
}

double scale (double x, int n)
{
        double scale_factor;
        scale_factor = pow(10, n);
        return (x*scale_factor);  
}

Hey guys, I'm trying to write a program that will take a positive number with a fractional part and round it up two places.For example 32.4851 would round to 32.49, and 32.4431 would round to 32.44.

I really am slightly lost on this one and was hoping you guys could help me out a little bit. I have written some code and need feedback (won't compile using gcc) using stdio.h and math.h

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

double x;

int rounded_x;

int main (void)
{
        printf ("Enter a number to be rounded \n\n\n\n\n");
        scanf ("&lf", &x);
        rounded_x=(int) (x+0.5);
        return 0;
}

double scale (double x, int n)
{
        double scale_factor;
        scale_factor = pow(10, n);
        return (x*scale_factor);  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

放赐 2024-10-24 03:57:06

乘以100、除以100的解法是正确的。但由于浮点数据类型的性质,这最终可能会产生欺骗性的结果。以 10 为基数的漂亮整数(例如“1.23”)并不总是转换为以 2 为基数的浮点存储格式。因此,您可能会发现四舍五入“1.2345”最终结果为“1.23”,如预期的那样,但“1.1234”最终将结果为“1.11989589285982959295892859289582958295”或其他疯狂的东西。

因此,如果这种准确性很重要 - 特别是如果您在任何计算中使用这些四舍五入的数字,那么您应该考虑以整数进行操作。

例如,在使用金钱时,开发人员通常使用美分而不是美元。因此,“$1.75”表示为“175”。这保证了精确到分。只有当你想向用户显示它时才除以 100。

The multiply by 100, divide by 100 solution is right. But this can end up with deceiving results because of the nature of floating-point datatypes. Nice round numbers in base-10, like "1.23" do not always translate to the base-2 floating point storage format. So you may find that rounding "1.2345" ends up as "1.23" as expected, but "1.1234" will end up as "1.11989589285982959295892859289582958295" or something crazy.

For this reason, if this kind of accuracy is important - especially if you are using these rounded numbers in any calculation, then you should consider operating in integers.

For example, when working with money, developers often work in cents instead of dollars. So, "$1.75" is represented as "175". This guarantees accuracy to the cent. Only divide by 100 when you want to display it to the user then.

尾戒 2024-10-24 03:57:06

真的很简单。您只需乘以 100,加上 0.5,调用round(),然后除以 100。

Pretty simple really. You just mutiply by 100, add 0.5, call round(), then divide by 100.

紧拥背影 2024-10-24 03:57:06

我认为你需要 double ceil(double x) 来获取最小值或 double Floor(double x) 来获取最大值,两者都包含在 math.h 中

I think you need double ceil(double x) to get the smallest value or double floor(double x) to get the largest value, both included in math.h

无名指的心愿 2024-10-24 03:57:06

顺便说一句

 scanf ("&lf", &x);

我想应该是

 scanf ("%lf", &x);

..

by the way

 scanf ("&lf", &x);

should be

 scanf ("%lf", &x);

i think..

孤城病女 2024-10-24 03:57:06

这取决于语言具体实现的特性;对于舍入,如果数字 3 为 5 或更多,请尝试向该值添加 0.01,然后将结果截断到仅定义 2 位小数的变量中(如果可能)。

This depends on the characteristics of a specific implementation of a language; for roundup if digit 3 is 5 or more try adding 0.01 to the value then truncate with the result in a variable that has only 2 decimal places defined if possible.

旧故 2024-10-24 03:57:06

最简单(但丑陋)的代码是:

 int main (void)

    {

    printf ("Enter a number to be rounded \n\n\n\n\n");

    scanf ("&lf", &x);

    rounded_x=((int)(x*100)/100.0);

    return 0;

    }

The simplest (but an ugly) code is:

 int main (void)

    {

    printf ("Enter a number to be rounded \n\n\n\n\n");

    scanf ("&lf", &x);

    rounded_x=((int)(x*100)/100.0);

    return 0;

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