我的数字一直在四舍五入?

时间:2014-10-14 03:21:49

标签: c++ rounding

我正在为我的课程编写一个简单的程序来计算和显示某些数字,而我的代码都没有问题,但是在显示屏上我的一些数字在我不想要的地方变得圆滑他们是。我将精度设置为两位小数,因为一些输入数字是小数,但是例如,如果答案是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;
}

3 个答案:

答案 0 :(得分:2)

它与精确度无关。你的代码错了。

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);

为另一个做同样的事情

答案 1 :(得分:-1)

欢迎来到“C / C ++浮动精准俱乐部”哥们:)。这就是计算机记下十进制数的方式:)

此链接应该有用:

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

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

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

这将成为你想要的技巧;)

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

答案 2 :(得分:-1)

欢迎来到浮点运算的世界。浮点的第一条规则是:不要将它用作货币。人们真正关心他们的钱会发生什么,而使用浮点运算,你会因为舍入错误和不精确而无法确保正确性。浮点数不能准确地表示我们在日常生活中使用的某些数字,因此您绝对不应该在需要准确性的情况下使用它们(如果这看起来很奇怪,请查看如何实现浮点数) )。

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

如果你有一个值得保证的值保证(这是一个更大的&#34;如果&#34;比它可能首先出现的那样),你可以使用long来表示该量子的倍数(例如,一分钱)。但在你这样做之前,请确保你永远不需要关心这个价值的一小部分(半分钱等)。