473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1421
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: john smith | last post by:
Hi, I would like to save a stream's output precision and then change the precision and then restore it. One example I found was using ios_base::fixed, and ios_base::floatfield. But on my machine...
3
by: rwawryk | last post by:
Hi, Does anybody know how to implement parametrized stream operator (such as setw, setfill)? I need to put into the stream variable of type char* without terminating NULL. It would be great if I...
5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
0
by: John Friedland | last post by:
'printf' allows an integer to be printed with a precision. "printf("|%5.2d|", 1)", for example, produces "| 01|". This doesn't seem to be possible with stream I/O - 'precision' only seems to...
9
by: barcaroller | last post by:
When I use an iostream manipulator in a cout statement, it seems to affect all subsequent cout statements. cout << x << endl; // dec by default cout << hex << x << endl; // hex cout <<...
4
by: banansol | last post by:
I read the thread Floating point rounding error and I saw Richard Heathfield's description with several lines like: 0.1 = 1/2 = 0.5 - too large 0.01 = 1/4 = 0.25 - too large 0.001 = 1/8 = 0.125...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
3
by: horstwalter | last post by:
I do create an image of a form: public static Image GetImage(byte data) { if (data == null) return null; MemoryStream ms = new MemoryStream(data); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.