在 C++ 中显示四舍五入的值而不是十进制的精确值

Displaying rounded off values instead of exact value in decimal in C++

我在相当不错的 Windows 10 PC 上使用 MS Visual C++ 6.0(根据我的工作需要)。

我在显示通过“cin >>”手动输入的小数点后的值时遇到问题,我已经尝试了通过 DSKVP 的问题 (Show two digits after decimal point in c++) 回答的修复,但它仍然不会显示小数值,它只是删除小数点后的数字 and/or 四舍五入。

#include <iostream>
#include <iomanip>    //for setprecision and fixed
using namespace std;  

int readResults()
{
    cout << setprecision(2);
    cout << "Scanned Value: ";
    double vrrm(0.0);
    cin >> vrrm;
    return vrrm;
}

int main()
{
    double vrrm ( readResults() );
    double vrwm ( readResults() );
    double vr   ( readResults() );
    double vrms ( readResults() );
    double io   ( readResults() );
    double vfm  ( readResults() );

    cout << "\nParameters \tScanned Values  \t Limits \t\tUnit" << endl;
    cout << "Vrmm \t \t" << fixed << setprecision (2) << vrrm << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vrwm \t \t" << fixed << setprecision (2) << vrwm << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vr \t \t"   << fixed << setprecision (2) << vr   << " \t\t\t 45.00 - 50.00 \t\tV" << endl;
    cout << "Vrms \t \t" << fixed << setprecision (2) << vrms << " \t\t\t 33.00 - 37.00 \t\tV" << endl;             
    cout << "Io \t \t"   << fixed << setprecision (2) << io   << " \t\t\t 00.90 - 1.50  \t\tA" << endl;
    cout << "Vfm \t \t"  << fixed << setprecision (2) << vfm  << " \t\t\t 00.95 - 1.05  \t\tV" << endl;
    cout << endl;
    
    if ( vrrm >= 45.00  &&  vrrm <= 50.00  &&
         vrwm >= 45.00  &&  vrwm <= 50.00  &&
         vr >= 45.00    &&  vr <= 50.00    &&
         vrms >= 33.00  &&  vrms <= 37.00  && 
         io >= 0.90     &&  io <= 1.50     &&
         vfm >= 0.95    &&  vfm <= 1.05       )
        
        cout << "PASS" << endl << endl;
        
    else
        cout << "FAIL" << endl << endl;
                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    return 0;
}

当我输入十进制值时,结果是这样的:

Scanned Value: 49.98
Scanned Value: 49.99
Scanned Value: 49.85
Scanned Value: 35.01
Scanned Value: 0.99
Scanned Value: 1.01

Parameters      Scanned Values           Limits                 Unit
Vrmm            49.00                    45.00 - 50.00          V
Vrwm            49.00                    45.00 - 50.00          V
Vr              49.00                    45.00 - 50.00          V
Vrms            35.00                    33.00 - 37.00          V
Io              0.00                     00.90 - 1.50           A
Vfm             1.00                     00.95 - 1.05           V

FAIL

Press any key to continue

我尝试将 Vrrm 初始化为 0.0(如此处所示),甚至尝试将其乘以 1.00,如 Can't get cout to display decimals c++ 中所示,但我相信它不适用于我在这里写的内容 - 仍然不会显示小数点后的数值。

我非常希望显示完整的值,并且程序能够正确输出 PASS(应该是)。你们谁介意向我解释一下我去哪里 wrong/refer 找一些好的 reference/source 资料吗?

我这周刚开始学习 C++,Alex 的教程正在做 很棒的 教育我的工作,他的工作是我现在的参考:https://www.learncpp.com/.

我测试了你的代码,我唯一改变的是 readResults() 的 return 类型从 intdouble。这样做后,我得到了预期的结果!