Cout Formatting Using iomanip

Page 1 of 1

1 Replies - 152 Views - Last Post: 04 October 2012 - 10:42 AM Rate Topic: -----

#1 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Cout Formatting Using iomanip

Posted 04 October 2012 - 10:35 AM

I am trying to take an integer that has a max value of 9999 (i.e. 543 or 1230) and display it as a double in the format "$05.43" or "$12.30" where there are always 4 decimals shown. This function takes this integer, divides by 100 and attempts to display it in the above format, but what ends up happening is this:

$0.00
: r


$0.01
: e


$0.11
: w


$1.11
: q


$11.11
: x



Process returned 0 (0x0) execution time : 57.650 s
Press any key to continue.


it only displays 4 digits when the integer actually has 4 digits in it, so i guess the padding is what is failing. any insight?

void MCounter::show()
{
    double z=double(x)/100;
    cout << "$";
    cout << fixed << setprecision(2);
    cout << setfill('0') << setw(4) << z;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Cout Formatting Using iomanip

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,293
  • Joined: 25-December 09

Re: Cout Formatting Using iomanip

Posted 04 October 2012 - 10:42 AM

Duplicate topic. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1