471,831 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1029

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

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.