在 C 中将 Double 转换为 Int 问题

发布于 2024-10-18 00:25:33 字数 1639 浏览 7 评论 0原文

int main(void)
{
    double dollars;
    int count;

    /* Get amount of money and make sure it's in range */
    printf("Enter an amount up to $100.00: ");
    scanf("%lf",&dollars);
    while(dollars <= 0 || dollars > 100) {
        printf("Re-enter an amount: ");
        scanf("%lf",&dollars);
    }

    /* Convert money to 2 decimal places */
    dollars = (int) (dollars * 100) / 100.00;
    printf("\nAmount entered: $%.2lf\n\n", dollars);
    printf("Change breakdown:\n");

    /* Determine amount of $20.00s */
    count = dollars / 20;
    if(count > 1)
        printf("%i $20.00s\n", count);
    else if(count == 1)
        printf("%i $20.00\n", count);
    dollars = dollars - (count * 20);

    /* Determine amount of $10.00s */
    count = dollars / 10;
    if(count > 1)
        printf("%i $10.00s\n", count);
    else if(count == 1)
        printf("%i $10.00\n", count);
    dollars = dollars - (count * 10);

    /* Determine amount of $5.00s */
    count = dollars / 5;
    if(count > 1)
        printf("%i $5.00s\n", count);
    else if(count == 1)
        printf("%i $5.00\n", count);
    dollars = dollars - (count * 5);

    /* Determine amount of $1.00s */
    count = dollars / 1;
    if(count > 1)
        printf("%i $1.00s\n", count);
    else if(count == 1)
        printf("%i $1.00\n", count);
    dollars = dollars - (count * 1);


    /* Determine amount of pennies */
    /* NOT WORKING if 55.41 is inputted count becomes 40 instead of 41 */
    count = dollars / 0.01;
    printf("\n\n%i",count);
    printf("\n%lf\n\n",dollars / 0.01);

    return 0;
}
int main(void)
{
    double dollars;
    int count;

    /* Get amount of money and make sure it's in range */
    printf("Enter an amount up to $100.00: ");
    scanf("%lf",&dollars);
    while(dollars <= 0 || dollars > 100) {
        printf("Re-enter an amount: ");
        scanf("%lf",&dollars);
    }

    /* Convert money to 2 decimal places */
    dollars = (int) (dollars * 100) / 100.00;
    printf("\nAmount entered: $%.2lf\n\n", dollars);
    printf("Change breakdown:\n");

    /* Determine amount of $20.00s */
    count = dollars / 20;
    if(count > 1)
        printf("%i $20.00s\n", count);
    else if(count == 1)
        printf("%i $20.00\n", count);
    dollars = dollars - (count * 20);

    /* Determine amount of $10.00s */
    count = dollars / 10;
    if(count > 1)
        printf("%i $10.00s\n", count);
    else if(count == 1)
        printf("%i $10.00\n", count);
    dollars = dollars - (count * 10);

    /* Determine amount of $5.00s */
    count = dollars / 5;
    if(count > 1)
        printf("%i $5.00s\n", count);
    else if(count == 1)
        printf("%i $5.00\n", count);
    dollars = dollars - (count * 5);

    /* Determine amount of $1.00s */
    count = dollars / 1;
    if(count > 1)
        printf("%i $1.00s\n", count);
    else if(count == 1)
        printf("%i $1.00\n", count);
    dollars = dollars - (count * 1);


    /* Determine amount of pennies */
    /* NOT WORKING if 55.41 is inputted count becomes 40 instead of 41 */
    count = dollars / 0.01;
    printf("\n\n%i",count);
    printf("\n%lf\n\n",dollars / 0.01);

    return 0;
}

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

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

发布评论

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

评论(2

完美的未来在梦里 2024-10-25 00:25:33

我同意使用整数而不是双精度数的评论,但快速解决方法是将计算便士的行从:

count = dollars / 0.01;

更改为:

count = round(dollars / 0.01);

I agree with the comment on using integers instead of doubles, but a quick fix is to change the line that calculate pennies from:

count = dollars / 0.01;

to:

count = round(dollars / 0.01);
缱绻入梦 2024-10-25 00:25:33

考虑使用整数。他们更容易合作,尤其是在金钱方面。

double input_dollars;
// ...
long dollars = input_dollars * 100 + 0.5;  // in cents

Consider working with integers. They're easier to work with, particularly when it comes to money.

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