c ++ cout<< [double]不打印小数位 [英] c++ cout << [double] not printing decimal places

查看:311
本文介绍了c ++ cout<< [double]不打印小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的C ++程序,我认为可以将f和g中定义的double值打印为double值,但是C ++会将它们打印为整数。我检查了cout的默认精度,当我运行它时,输出显示默认精度应该为6,所以我对为什么double不能正确打印感到困惑。

I have a simple C++ program that I thought would print out a double value defined in f and g as doubles ... but C++ is printing them out as integers. I checked the default precision of cout, and when I run it the output says the default precision should be 6 so I'm confused as to why the double isn't printing properly.

#include <iostream>
#include <stdio.h>
int main() {

    double f = 735416.416898;
    double g = f + 0.3;

    std::cout << "default precision is " << std::cout.precision() << std::endl;

    std::cout << "[c++] f = " << f << std::endl;
    std::cout << "[c++] g = " << g << std::endl;
    printf("[c] f = %f\n", f);
    printf("[c] g = %f\n", g);

    f = 735416.516898;
    g = f + 0.3;

    std::cout << "[c++] f = " << f << std::endl;
    std::cout << "[c++] g = " << g << std::endl;
    printf("[c] f = %f\n", f);
    printf("[c] g = %f\n", g);

    return 0;

}

我看到的输出是:

kiran@kiran-VirtualBox:~/test$ g++ -o test test.cpp 
kiran@kiran-VirtualBox:~/test$ ./test 
[c++] f = 735416
[c++] g = 735417
[c] f = 735416.416898
[c] g = 735416.716898
[c++] f = 735417
[c++] g = 735417
[c] f = 735416.516898
[c] g = 735416.816898

因为默认精度显示为6,所以cout难道不也应该显示小数点后6位吗?

Because the default precision is showing up as 6, shouldn't the cout also be showing the 6 decimal places?

如果我愿意

std::cout << std::fixed << f

打印正确(带有所有小数位)。

it prints properly (w/ all the decimal places).

只是想知道是否有人可以向我解释这种行为?是预期的吗?我是否应该使用std :: fixed来强制正确打印双打?

Just wondering if someone can explain this behavior to me? Is it expected? Am I expected to use std::fixed to force printing of doubles properly?

推荐答案

这是由于默认值之间的区别而发生的符号和固定符号。

This happens because of the distinction between the default notation and the fixed notation.

根据文档



  • 使用默认浮点符号,precision字段指定有意义的最大数量要显示的数字总计小数点之前和之后的数字。请注意,这不是最小值,因此,如果显示的数字位数可能少于精度,则不会在显示的数字后缀零。

  • 在固定数和科学数中表示法,精度字段精确指定要在小数点后显示的位数,即使其中包括尾随的十进制零。在这种情况下,小数点前的数字

    (加重)无关。

  • Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
  • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case (emphasis is added).

流将使用默认的浮点符号,直到(或除非)将其切换为固定或科学符号。由于您的数字在小数点前有六位数字,并且默认精度也设置为六,因此浮点数看起来像整数。

The stream uses the default floating-point notation until (or unless) you switch it to fixed or scientific notation. Since your number has six digit before the decimal point, and the default precision is also set to six, the floating-point numbers look like integers.

这篇关于c ++ cout&lt;&lt; [double]不打印小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆