【问题标题】:Digit limitation from decimal point in C++C ++中小数点的数字限制
【发布时间】:2010-10-22 08:00:42
【问题描述】:

我是 C++ 新手。我有一个双变量double a=0.1239857,我想将变量a 限制为小数点两位数。所以a 将是0.12。我知道 C++ 具有返回大于或小于a 的最大或最小整数的函数,例如 ceil 或 floor。

有没有实现浮点变量位数限制的函数?或者如何更改a 变量的精度?

【问题讨论】:

标签: c++


【解决方案1】:

你想限制变量是什么意思?值或其格式。对于值,您可以使用地板 + 除法。比如:

double a = 0.12123
double b;

b = floor(a * 100) / 100

【讨论】:

    【解决方案2】:

    这将导致小数点后两位数。

    a = floor(a * 100.0) / 100.0;
    

    【讨论】:

    • 虽然 OP 没有询问:请注意,在打印 a 时,不能保证只显示 2 位数字。会有舍入误差。
    【解决方案3】:

    您实际上是在尝试对数字进行四舍五入,还是只是更改其显示的精度?

    对于前者(截去多余的数字):

    double scale = 0.01;  // i.e. round to nearest one-hundreth
    value = (int)(value / scale) * scale;
    

    或(根据 jheriko 的回答,适当向上/向下四舍五入)

    double scale = 0.01;  // i.e. round to nearest one-hundreth
    value = floor(value / scale + 0.5) * scale;
    

    对于后者:

    cout << setprecision(2) << value;
    

    setprecision() 的参数是小数点后显示的最大位数。

    【讨论】:

    • 请注意,当您在显示的流上使用 setprecision 时,它也会舍入。
    • 布赖恩 - 请详细说明。当然,它会显示向上或向下舍入到适当位数的值,但不会更改实际存储在变量中的值。
    • 显示到 cout 时,该值将四舍五入到适当的精度。当然对变量中的值没有影响。
    • 我想说它两者兼而有之:setprecision 将值显示为所需的精度,and 在这样做的同时进行舍入。
    • 您也需要使用“固定”。 cout &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; value;
    【解决方案4】:

    如果你只想输出值,你可以这样做

    printf("%.3f", a); // Output value with 3 digits after comma
    

    如果你想转换值本身,你可以这样做:

    a = (int)(a * 1000) / 1000.0f;
    

    请注意,两者都不进行四舍五入,它们只是截断值。

    【讨论】:

      【解决方案5】:

      使用 ios_base::precision 格式化 i/o。

      【讨论】:

        【解决方案6】:

        您可以在流上设置精度,例如

        double d = 3.14579;
        cout.precision(2);
        cout << d << endl;
        
        // Or use a manipulator
        
        #include <iomanip>
        cout << setprecision(2) << d << endl;
        

        请注意,当您将双精度或浮点数发送到这样的流时,它会自动为您舍入(如果您不知道这一点,有时可能会绊倒您)。

        【讨论】:

          【解决方案7】:

          假设要舍入的值在变量“x”中,则实际舍入解决方案是x = floor(100*x + 0.5) / 100;

          这里其他人推荐的x = floor(100*x) / 100;实际上会将数字截断为2dp。

          【讨论】:

          • 我无法理解 linefloor(100*x + 0.5) / 100
          【解决方案8】:

          你也可以这样做:

          //This code will ask the user for an input, set the decimal precision to the hundredths place,  and add 4.63 to the inputted variable
          
          int banana;
          cin >> banana;
          cout << setprecision(2) << fixed << banana + 4.63; 
          

          【讨论】:

            【解决方案9】:

            您可以编写自己的函数如下,它也可以处理小数的舍入错误。

            double RoundForGivenPrecision(const double dNumber, int iDecimalPlaces)
            {
            long long multiplier = (long long)pow(10, iDecimalPlaces);
            long double value = dNumber < 0 ? (long long)((nextafter(dNumber, -DBL_MAX)*multiplier)-0.5) : (long long)((nextafter(dNumber,DBL_MAX)*multiplier)+0.5);
            return value / multiplier;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-05-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多