C++ 将双精度舍入到小数点后两位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25925290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:28:41  来源:igfitidea点击:

C++ round a double up to 2 decimal places

c++roundingcinceil

提问by Bryce Hahn

I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368)

我无法将 GPA 双精度舍入到小数点后两位。(前 GPA 需要四舍五入:3.67924)我目前正在使用 ceil 进行四舍五入,但它目前将其输出为整数 (368)

here is what I have right now

这是我现在拥有的

if (cin >> gpa) {
    if (gpa >= 0 && gpa <= 5) {
           // valid number

           gpa = ceil(gpa * 100);

           break;
    } else {
           cout << "Please enter a valid GPA (0.00 - 5.00)" << endl;
           cout << "GPA: ";

    }
}

using the above code with 3.67924 would output 368 (which is what I want, but just without the period between the whole number and the decimals). How can I fix this?

将上面的代码与 3.67924 一起使用将输出 368(这是我想要的,但只是没有整数和小数之间的句点)。我怎样才能解决这个问题?

回答by AndersK

If it is just a matter of writing to screen then to round the number use

如果这只是写屏幕的问题,那么将数字四舍五入使用

std::cout.precision(3);
std::cout << gpa << std::endl;

see

floating points are not exactly represented so by internally rounding the value and then using that in your calculations you are increasing the inexactness.

浮点数不能完全表示,因此通过内部舍入值然后在计算中使用它会增加不精确性。

回答by kaveish

To round a double up to 2 decimal places, you can use:

要将双精度舍入到小数点后两位,您可以使用:

#include <iostream>
#include <cmath>

int main() {
    double value = 0.123;
    value = std::ceil(value * 100.0) / 100.0;
    std::cout << value << std::endl; // prints 0.13
    return 0;
}

To round up to n decimal places, you can use:

要四舍五入到 n 个小数位,您可以使用:

double round_up(double value, int decimal_places) {
    const double multiplier = std::pow(10.0, decimal_places);
    return std::ceil(value * multiplier) / multiplier;
}

This method won't be particularly fast, if performance becomes an issue you may need another solution.

这种方法不会特别快,如果性能成为问题,您可能需要其他解决方案。

回答by MD. Khairul Basar

Try this. But your coutstatement in else condition, so it won't give the desired output for 3.67924.

尝试这个。但是您cout在 else 条件下的语句,因此不会为 3.67924 提供所需的输出。

if (cin >> gpa)
{     
    if (gpa >= 0 && gpa <= 5) {
        // valid number

        gpa = ceil(gpa * 100);
        gpa=gpa/100;
        break;
    } 
    else
    {    
       cout << "Please enter a valid GPA (0.00 - 5.00)" << endl;    
       cout << "GPA: ";
    }
}

回答by user207421

You can't round doubles to two decimal places. Doubles don't havedecimal places. They have binary places, and they aren't commensurable with decimal places.

您不能将双精度舍入到两位小数。双打不具有小数。它们有二进制位,并且它们与小数位不相称。

If you want decimal places, you must use a decimal radix, e.g. when formatting for output with printf("%.2f", ...).

如果您想要小数位,则必须使用小数基数,例如在使用 printf("%.2f", ...) 格式化输出时。

回答by Goh Allant

Example: you want 56.899999999999 to be output as a string with 2 decimal point which is 56.89.

示例:您希望将 56.899999999999 输出为带有 2 个小数点的字符串,即 56.89。

First, convert them
value = 56.89 * 1000 = 5689
factor = 100
- 1 decimal point = 10
- 2 decimal point = 100
- 3 decimal point = 1000
etc

首先,将它们转换为
value = 56.89 * 1000 = 5689
factor = 100
- 1 个小数点 = 10
- 2 个小数点 = 100
- 3 个小数点 = 1000
等等

int integerValue;
int decimal;
std::string result;
function ( int value , int factor)
{
    integerValue= (value / factor) * factor; //(5689 / 100) * 100 = 5600
    decimal = value - integerValue;  // 5689 - 5600;
    result = std::to_string((int)(value/factor) + "." + std::to_string(decimal); 
    // result = "56" + "." + "89" 
    // lastly, print result
}

Not sure if this can help?

不确定这是否有帮助?