471,066 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

stream manipulator for set precision closing zeroes?

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==]
Nov 17 '05 #1
2 1353
Peteroid wrote:
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
Nov 17 '05 #2
Hi Tom,

Thanks! :)

[==Peteroid==]

"Tom Widmer" <to********@hotmail.com> wrote in message
news:ux**************@tk2msftngp13.phx.gbl...
Peteroid wrote:
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by john smith | last post: by
3 posts views Thread by rwawryk | last post: by
reply views Thread by John Friedland | last post: by
9 posts views Thread by barcaroller | last post: by
4 posts views Thread by banansol | last post: by
6 posts views Thread by Dan Smithers | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.