首页 技术资料正文

C++fixed用法详解(python爬虫)

piaodoo 技术资料 2022-08-26 22:10:09 660 0

C++ fixed用法详解(python爬虫)

C++ fixed用法详解

如果一个数字太大,无法使用 setprecision 指定的有效数位数来打印,则许多系统会以科学表示法的方式打印。例如,还是之前的程序:

// This program asks for sales figures for three days.
// The total sales are calculated and displayed in a table.
include 
include  // Header file needed to use stream manipulators
using namespace std;

int main()
{
    double day1, day2, day3, total;
    // Get the sales for each day
    cout << "Enter the sales for day 1: ";
    cin >> dayl;
    cout << "Enter the sales for day 2: ”;
    cin >> day2;
    cout << "Enter the sales for day 3: ”;
    cin >> day3;
    // Calculate total sales
    total = day1 + day2 + day3;
    // Display the sales figures
    cout << "\nSales Figures\n";
    cout << "-------------\n" ;
    cout << setprecision (5);
    cout << "Day 1: " << setw(8) << day1 << endl;
    cout << "Day 2: " << setw(8) << day2 << endl;
    cout << "Day 3: " << setw(8) << day3 << endl;
    cout << "Total: " << setw(8) << total << endl;
    return 0;
}
以下是在该程序输入较大数字之后的输出:

Enter the sales for day 1: 145678.99Enter the sales for day 2: 205614.85Enter the sales for day 3: 198645.22Sales Figures-------------Day 1: 1.4568e+005Day 2: 2.0561e+005Day 3: 1.9865e+005Total: 5.4994e+005调频耳机怎么使用

为了防止出现这种情况,可以使用另一个,它表示浮点输出应该以固定点或小数点表示法显示:

cout << fixed;

当然,fixed 操作符可能最重要的还是当它与 setprecision 操作符一起使用时,setprecision 即可以以一种新的方式显示。它将指定浮点数字的小数点后要显示的位数,而不是要显示的总有效数位数。而这通常正是我们想要的。例如,可以重写上面程序的第 22 行如下:

cout << fixed << setprecision(2);

然后使用相同的样本数据重新运行程序,即可得到以下结果:
Enter the sales for day 1: 321.57
Enter the sales for day 2: 269. 60
Enter the sales for day 3: 307.00
Sales Figures
-------------
Day 1:   321.57
Day 2:   269.60
Day 3:   307.00
Total:   898.17
通过将 fixed 和 setprecision 结合起来使用,得到了所需的输出结果。请注意,在这种情况下,精度值应设置为 2,这是希望看到的小数位数,而不是 5。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/119262.html

评论

搜索