how to add a ".0" to ostream floats

G

Gianni Mariani

I need to write float values in a specific format.


#include <iostream>
#include <iomanip>

void outdbl( double d )
{
std::cout
<< std::setiosflags(
std::ios_base::showpoint | std::ios_base::right
)
<< std::setprecision(16)
<< d
<< "\n";
}


int main()
{
outdbl( 22.0 );
outdbl( 1.111111111111111111111111 );
}

------------
I would like to see the output be:

22.0
1.111111111111111? ... + whatever the roundoff gives as the last digit


I can't seem to find the incantation if iosflags that that does this.
 
D

Denise Kleingeist

Hello Gianni!
Gianni said:
I would like to see the output be:

22.0
1.111111111111111? ... + whatever the roundoff gives as the last digit

Without changing the formatting flags to adapt to the situation, this
will never
happen: you will get 16 digits in either case unless you change the
precision
in cases where you don't want that many trailing zeros. Other than
that,
the formatting flag you are looking for is "fixed".

If you really want the output as indicated above and you really want to
use
simple output to take care of that, you can replace the
std::num_put<char>
facet by a custom facet which does the formatting for you. Of course,
you
still need to write the facet in that case.

Good luck, Denise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top