首页 文章

金钱计数面额C.

提问于
浏览
-2

所以我的项目是开发一个应用程序,用户输入一个数量(例如65.67),输出打印多少美元,四分之一,一角钱,镍币和便士总数(例如5.52美元是5美元,2个季度下面我已经离开了代码块来处理总数的计算 . 美元和四分之一的分配正确,但硬币,镍币和便士都没有 . 我的逻辑是关闭的,任何建议都将受到高度赞赏我正在使用Windows 8.1,Code :: Blocks IDE .

// Calculations for money denomination//
    float dollar = floor(total);
        total = total-dollar;
    float quarter = floor(total/.25);
        total= total -(quarter*.25);
    float dime = floor(total/.1);
        total = total-(dime*.1);
    float nickel = floor(total/.05);
        total = total-(total*.05);
    float penny = floor(total/.01);
        total = total-(total*.01);

2 回答

  • 0

    使用货币值应该使用整数来完成,因为在对它们执行计算操作时,无法保证浮点值正确舍入 .

    基本上你应该使用尽可能小的货币单位(美分)并管理这些单位的所有货币 Value . 显示值时,您将转换为用户期望的正确格式 .

  • -1

    谢谢大家的建议,这是我最终做的 . 不是我确定的最优雅的方式,但它完成了工作 .

    /// This states variables as well as evolving tally of dollar amount ///
    
    int _total = (total*100)+.5; 
    int dollar = _total/100;
     _total = _total%100;
    int quarter = _total/25;
     _total= _total%25;
    int dime = _total/10;
     _total = _total%10;
    int nickel = _total/5;
     _total = _total%5;
    int penny = _total/1;
     _total = _total%1;
    

相关问题