I want a number to be displayed with a positive sign and three 0's preceding it, but what I am getting so far is 000+1 when what I want is +0001
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
int number = 1;
cout << showpos;
cout << setfill('0') << setw(5) << number << endl;
}
You need to also set std::internal
flag. This way you will get your expected +0001
- test at ideone.
This is what the std::internal manipulator is for. For example,
std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << std::endl;
prints "-0005" instead of "000-5" as without std::internal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With