【问题标题】:My numbers keep rounding?我的数字一直在四舍五入?
【发布时间】:2014-10-14 03:21:48
【问题描述】:

我正在为我的班级编写一个简单的程序来计算和显示某些数字,并且我的代码都可以正常运行,但是在显示屏上我的一些数字在我不希望它们出现的地方被四舍五入。我将精度设置为两位小数,因为一些输入数字是小数,但例如,如果答案是 3000,则显示数字 2999。

这是我的代码,如果有帮助的话

int main()
{

    double expense = 0.0;
    double expenseTotal = 0.0;
    double income = 0.0;
    double incomeTotal = 0.0;

    double netProfit = 0.0;
    double netIncome = 0.0;
    double netExpense = 0.0;

    int incNo = -1;
    int expNo = -1;

    cout << "ENTER A NEGATIVE NUMBER TO STOP INCOME AMOUNT DATA ENTRY." << endl << endl;

    do
    {
        cout << "Enter Income Amount: ";
        cin >> income;
        netIncome = income;
        incomeTotal += netIncome;
        incNo += 1;

    } while (income > 0);

    cout << endl << "ENTER A NEGATIVE NUMBER TO STOP EXPENSE AMOUNT DATA ENTRY." << endl << endl;

    do
    {
        cout << "Enter Expense Amount: ";
        cin >> expense;
        netExpense = expense;
        expenseTotal += netExpense;
        expNo += 1;

    } while (expense > 0);


    cout << endl << "Total of the " << incNo << " income amounts entered ----> $" << fixed << setprecision(2) << incomeTotal << endl;
    cout <<"Total of the " << expNo << " expense amounts entered ----> $" << fixed << setprecision(2) << expenseTotal << endl;
    netProfit = incomeTotal - expenseTotal;

    if (netProfit > 0)
        {
            cout << "Net Profit earned ----> $" << fixed << setprecision(2) << netProfit;
            cout << endl;
        }

    else
        {
        cout << "Net Loss incurred ----> $" << fixed << setprecision(2) << netProfit;
        cout << endl;
        cout << "Note . . . Loss figire most be reported to company CEO." << endl << endl;
        }


    return 0;
}

【问题讨论】:

  • 您使用的浮点数会导致精度损失(完整讨论请参阅this link)。您的结果被截断,因此结果很奇怪。将您的输入乘以 100 并截断,然后在最后转换回来之前以整数进行所有算术运算。
  • 不正确,代码错误。看我的回答

标签: c++ rounding


【解决方案1】:

这与精度无关。你的代码是错误的。

do {
    cout << "Enter Expense Amount: ";
    cin >> expense;             // --> what if users enter negative value to quit?
    netExpense = expense;       // --> hint: netExpense = netExpense + (negative value)
    expenseTotal += netExpense; // --> hint: expenseTotal = expenseTotal + (negative value)
    expNo += 1;
} while (expense > 0);

修复:

do {
    cout << "Enter Income Amount: ";
    cin >> income;
    if (income > 0){                // now when the input is negative
        netIncome = income;         // it will not change anything but quit
        incomeTotal += netIncome;
        incNo += 1;
    }     
} while (income > 0);

对另一个做同样的事情

【讨论】:

    【解决方案2】:

    欢迎来到浮点运算的世界。浮点数的第一条规则是:不要将它用于货币。人们确实关心他们的钱会发生什么,并且使用浮点运算,由于舍入错误和不精确性,您将在确保正确性方面遇到无穷无尽的麻烦。浮点数根本无法准确地表示我们日常生活中使用的某些数字,因此您绝对不应该在需要准确性的场景中使用它们(如果这看起来很奇怪,请查看浮点数是如何实现的)。

    那么你应该使用什么?好吧,几乎所有语言都有专门的货币库,但您也可以使用任意精度的库,如 GMP,其功能类似于 Java 的 BigDecimal 库。

    如果您有一个您关心的有保证的价值量(这比最初出现的“如果”更大),您可以使用long 来表示该量的倍数(例如,一美分) .但在您这样做之前,请确保您永远不需要关心该价值的一小部分(半美分等)。

    【讨论】:

      【解决方案3】:

      欢迎来到“C/C++ 浮动精度俱乐部”好友 :) 。这只是计算机写下十进制数字的方式:)

      这个链接应该很有用:

      http://www.parashift.com/c++-faq-lite/floating-pt-errs.html

      顺便说一句,printf 做得更好一点,试试这个:

             printf("%.2f", netProfit);
      

      这会达到你想要的效果;)

      @Limantara:上面代码中的代码对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-12
        • 2011-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        相关资源
        最近更新 更多