I want to print out the entire 'double' when I print out the cout

Asked 1 years ago, Updated 1 years ago, 70 views

If I run the source code below, It says 3.14159265358979 instead of 3.14159 Instead of rounding off the decimal point, What should I do to get the full price?

int main(){
    double d = 3.14159265358979;
    cout << d << endl;
}

c++ floating-point precision iostream cout

2022-09-21 21:25

1 Answers

std::cout Set how many decimal places to output You can write std::fixed format specifier

int main(){
    double d = 3.14159265358979;
    cout.precision(11);
    cout << fixed << d << endl;
}

Result: 3.14159265359

If you want to print out everything without leaving anything behind, <limits> indicates up to what decimal point it supports.

#include <limits>
typedef std::numeric_limits< double > dbl;

int main(){
    double d = 3.14159265358979;
    cout.precision(dbl::max_digits10);
    cout << fixed << d << endl;

}

Result: 3.14159265358979001


2022-09-21 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.