Set digits accuracy to decimal point?

I already looked at this thread , but it does not account for the use of negative numbers if I use setfill ('0').

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    //Code
    int num1;

    cin >> num1;

    cout << setw(5) << setfill( '0' ) << num1 << endl;

    return 0;
}

Using this method, if I enter “5”, it will display as “00005”. But if I enter -5, it will display as "000-5"

How can I fix this so that out-put is "-0005"?

+3
source share
1 answer

" " . , . std::left . std::internal :

cout << setw(5) << setfill('0') << internal << -42 << endl;

http://ideone.com/Pdsz3M

+4

All Articles