简体   繁体   English

如何在C ++中正确设置双精度的精度

[英]How to properly set precision for doubles in C++

I'm working on a project where I need to do some math and give the user output with dollars in it, so I would like to have my console tell the user an answer like $20.15 instead of $20.153 . 我正在做一个项目,我需要做一些数学运算,并在其中提供带美元的用户输出,因此我想让控制台告诉用户一个像$20.15而不是$20.153 I used the set precision function as such: cout << setprecision(2); 我这样使用了set precision函数: cout << setprecision(2); , but rather than have the numbers become what I want them to be, they are converted into scientific notation. ,而不是让数字变成我想要的数字,而是将其转换为科学计数法。

I'm outputting a lot of numbers, so having a function like setprecision would be best for me for ease of use. 我输出了很多数字,因此对于我来说,使用setprecision类的功能对我来说是最好的。

How do I properly have the numbers displayed with only two decimal places and not have the console give me numbers in scientific notation? 如何正确显示仅用小数点后两位数字显示的数字,而不使控制台以科学计数形式显示数字?

Thanks 谢谢

Nathan 弥敦道

EDIT: 编辑:

Here is the part of my code I'm having problems with: 这是我遇到问题的部分代码:

int main() {

cout << setprecision(2);

if (totalCostHybrid < totalCostNonHybrid) {
            cout << "Hybrid car: " << endl;
            cout << "Total cost: " << totalCostHybrid << endl;
            cout << "Total gallons used: " << milesPerYear / hybridEffic << endl;
            cout << "Total gas cost: " << gasCostHybrid << endl;
            cout << "Non-hybrid car: " << endl;
            cout << "Total cost: " << totalCostNonHybrid << endl;
            cout << "Total gallons used: " << milesPerYear / nonHybridEffic << endl;
            cout << "Total gas cost: " << gasCostNonHybrid << endl;
            cout << "Hybrid is cheaper!" << endl;
}

Obviously there's more to it, but this is what I need help with. 显然还有更多,但这是我需要帮助的。

Iostreams are a pain for formatting floating-point values. Iostream是格式化浮点值的痛苦。 But why are you using floating-point to represent currency values? 但是,为什么要使用浮点数来表示货币值? You should store integer pennies (or tenth-pennies) because, though you're not measuring in whole numbers of dollars, your values are actually fixed-point. 您应该存储整数美分(或十分之一美分),因为尽管您没有用整数来衡量,但您的值实际上是定点的。 And you really don't need the trouble that floating-point brings. 您真的不需要浮点运算带来的麻烦。 And then you can stream the whole and "fractional" parts of your value separately (use / and % !), as integers, with a '.' 然后,您可以分别将值的整个和“小数”部分(使用/% !)以整数形式用'.' in the middle. 在中间。

In the meantime, try std::fixed . 同时,尝试使用std::fixed

To fix that, you should use fixed floating-point notation for cout. 要解决此问题,您应该对cout使用固定的浮点符号。 You can find more info here . 您可以在此处找到更多信息。

Try addind cout << fixed to your code, like the code below. 尝试将addind cout << fixed到您的代码,例如下面的代码。 To set the precision to 2 , you can use the precision property. 要将precision设置为2 ,可以使用precision属性。

cout << fixed;
cout.precision(2);

Here is the complete code: 这是完整的代码:

using namespace std;

int main() {

    cout << fixed;
    cout.precision(2);

    if (totalCostHybrid < totalCostNonHybrid) {
            cout << "Hybrid car: " << endl;
            cout << "Total cost: " << totalCostHybrid << endl;
            cout << "Total gallons used: " << milesPerYear / hybridEffic << endl;
            cout << "Total gas cost: " << gasCostHybrid << endl;
            cout << "Non-hybrid car: " << endl;
            cout << "Total cost: " << totalCostNonHybrid << endl;
            cout << "Total gallons used: " << milesPerYear / nonHybridEffic << endl;
            cout << "Total gas cost: " << gasCostNonHybrid << endl;
            cout << "Hybrid is cheaper!" << endl;
     }
}

Cheat and watch purists go crazy... 作弊和看纯粹主义者发疯...

    double time;  //Only want two decimal places.
    double timeCon = time * 100.0;  //Pull out the two decimals I want.
    int timeCut = timeCon;  //Cut all decimal values.
    double timeRevert = timeCut / 100.0;  //Laugh.
    cout << timeRevert << endl;  //Watch heads explode.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM