stream manipulator for set precision closing zeroes?

  • Thread starter Thread starter Peteroid
  • Start date Start date
P

Peteroid

In the same spirit as setw( ) and setprecision( ), is there a stream
manipulator that will fill in closing-zeroes on those fractions with a given
setprecision( )?

For example, if setprecision(3) is used, I want '1' to be displayed as
"1.000", '1.2' to be displayed as "1.200", '1.23' to be displayed as
"1.230", '1.234' to be displayed as "1.234", and '1.2345' to be displayed as
"1.234" (or the rounded version "1.235" is ok too).

Thanx in advance!!!

[==Peteroid==]
 
Peteroid said:
In the same spirit as setw( ) and setprecision( ), is there a stream
manipulator that will fill in closing-zeroes on those fractions with a given
setprecision( )?

For example, if setprecision(3) is used, I want '1' to be displayed as
"1.000", '1.2' to be displayed as "1.200", '1.23' to be displayed as
"1.230", '1.234' to be displayed as "1.234", and '1.2345' to be displayed as
"1.234" (or the rounded version "1.235" is ok too).

std::fixed and std::ios_base::fixed are what you want. e.g.

#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::setprecision(3) << std::fixed
<< 1.23456 << ' ' << 1.2 << '\n';
}

Tom
 
Hi Tom,

Thanks! :)

[==Peteroid==]

Tom Widmer said:
std::fixed and std::ios_base::fixed are what you want. e.g.

#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::setprecision(3) << std::fixed
<< 1.23456 << ' ' << 1.2 << '\n';
}

Tom
 

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

Back
Top