How do I make "std::cout << 123456789.12" print "123456789.12"?
Asked Answered
P

3

6

How do I make

std::cout << 123456789.12

print this:

123456789.12

It always prints this:

1.23457e+008

I know that I have to play with the flags, but I cant quite figure out the right combination. If I set the fixed flag, it prints

123456789.120000
Pigeonhole answered 13/3, 2014 at 15:25 Comment(7)
std::setprecision : en.cppreference.com/w/cpp/io/manip/setprecisionSystemic
??? NEW CPP programmer why did u add the STL Flag ?? Anyways u can set the precision by std::setprecision(int ) try std::cout << std::setprecission(20) << 123456789.12 << endl;Hagerty
See #22178156Skink
What do you want to see in general? Fixed notation and 2 digits after the point? 11 digits in total?Meaningful
Heh. The smart-alec answer would be to put the number in quotes: std::cout << "123456789.12";Brachyuran
I just want the trailing zeros to vanish. But I want it to show all digits necessary. So, if there are three digits after the point, I want it to show them.Pigeonhole
"if there are three digits after the point, I want it to show them" -- zero is a digit. Suppressing trailing zeros is usually both difficult and unwise.Billingsley
B
1

You can use:

#include <iostream>
#include <limits>
using namespace std;

int main() {
    double c = 123456789.12;
    cout.precision(numeric_limits<double>::digits10 + 1);
    cout << c << endl;

    return 0;
}

Basically the limits package has traits for all the build-in types. One of the traits for floating point numbers (float/double/long double) is the digits10 attribute. This defines the accuracy of a floating point number in base 10.

See it live: http://ideone.com/Ity9m7


To read on, check out another similar question: How do I print a double value with full precision using cout?

Backstop answered 13/3, 2014 at 15:32 Comment(1)
Note, the final digit you get from +1 isn't to be taken as useful information, except to be able to serialize and restore the value exactly.Cide
S
11

How to ... ?

One way :-

#include <iostream>
#include <iomanip>

int main() {
    double f =123456789.12;
    std::cout << std::fixed << std::setprecision(2) << f << '\n';
    return 0;
}

See here

Please look for appropriate references

Shill answered 13/3, 2014 at 15:31 Comment(0)
B
1

You can use:

#include <iostream>
#include <limits>
using namespace std;

int main() {
    double c = 123456789.12;
    cout.precision(numeric_limits<double>::digits10 + 1);
    cout << c << endl;

    return 0;
}

Basically the limits package has traits for all the build-in types. One of the traits for floating point numbers (float/double/long double) is the digits10 attribute. This defines the accuracy of a floating point number in base 10.

See it live: http://ideone.com/Ity9m7


To read on, check out another similar question: How do I print a double value with full precision using cout?

Backstop answered 13/3, 2014 at 15:32 Comment(1)
Note, the final digit you get from +1 isn't to be taken as useful information, except to be able to serialize and restore the value exactly.Cide
G
0

You can use boost::lexical_cast as follow:

#include <boost/lexical_cast.hpp>

std::cout << boost::lexical_cast<std::string>(123456789.12);

more info can be found in http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html

Gow answered 13/3, 2014 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.