473,796 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1431
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::setprecisi on(3) << std::fixed
<< 1.23456 << ' ' << 1.2 << '\n';
}

Tom
Nov 17 '05 #2
Hi Tom,

Thanks! :)

[==Peteroid==]

"Tom Widmer" <to********@hot mail.com> wrote in message
news:ux******** ******@tk2msftn gp13.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::setprecisi on(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
5389
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 this only gives 6 or 8 digits after the decimal. I would like at least 12. If I do cout.precision(12), then cout is forever set to 12 precisions from that point on. I guess I can call cout.precision(4) when I'm done, but how do I know that 4...
3
3348
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 had the manipulator which allows to determine maximum number of characters ( let's assume maxw( char* str, int maxlen ) ). example: cout << maxw("Crocodile", 5 );
5
2179
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: http://groups.google.fi/group/comp.lang.c++/browse_frm/thread/cc57b579d6832732/530bae667a479add#530bae667a479add So, the basic idea is to have a compact logger class which can be used like the std::cout stream and add extra functionality to it (line numbering, some output counters etc.).
8
1800
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello( ostream & os ) { return os << "regular: hello"; }
0
1122
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 be relevant to floating-point numbers. Can anyone tell me if I can do the above with stream I/O? I've attached a simple test program; the output is | 01|
9
3976
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 << x << endl // still hex Is there a way I can have manipulators affect only the "current" cout statement?
4
2267
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 - too large .... And I wanted to write such a program that output lines like that. My attempt is below, and I don't
6
2612
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 inside template class definition (commented out at //1) then it compiles and runs fine, but if I declare and define the function separately (at //2). Is the following syntax supported by g++?
3
10116
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); Image img = Image.FromStream(ms); // closing the stream later crashes the application // ms.Close(); // => memory leak return img;
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.