在 C 中将 Double 转换为 Int 问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意使用整数而不是双精度数的评论,但快速解决方法是将计算便士的行从:
更改为:
I agree with the comment on using integers instead of doubles, but a quick fix is to change the line that calculate pennies from:
to:
考虑使用整数。他们更容易合作,尤其是在金钱方面。
Consider working with integers. They're easier to work with, particularly when it comes to money.