468,484 Members | 1,765 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Specifying integer precision with Stream I/O

'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|
| 1|
|00001|

Thanks -

John

----------------------------------------
#include <stdio.h>
#include <iostream>
#include <iomanip>
int main(void) {
int a = 1;

printf("|%5.2d|\n", a);
std::cout
<< '|' << std::setw(5) << std::setprecision(2) << a
<< '|' << std::endl;
std::cout
<< '|' << std::setw(5) << std::setprecision(2)
<< std::setfill('0') << a << '|' << std::endl;
}
----------------------------------------
Jul 21 '06 #1
0 965

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by john smith | last post: by
19 posts views Thread by Robbie Hatley | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
10 posts views Thread by Bo Yang | last post: by
39 posts views Thread by rembremading | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.