decimal places

This is a discussion on decimal places within the C++ Programming forums, part of the General Programming Boards category; //intarith.cpp// //convert cents to dollars and cents// I am trying to print the result with two decimal places but it ...

  1. #1
    Unregistered
    Guest

    decimal places

    //intarith.cpp//
    //convert cents to dollars and cents//

    I am trying to print the result with two decimal places but it is not working.

    I get a result of 1234.6 but i want 1234.06 the 0 does not print

    Help!



    #include <iostream.h>
    #include <iomanip.h>
    #define cents_per_dollars 100 //cents in dollars//

    int main (void)
    {
    long cents;
    long d, c;
    cents=123406;

    d=cents/cents_per_dollars;
    c=cents%cents_per_dollars;

    cout.setf(ios::fixed,ios::floatfield);
    cout<<setprecision(2)<<"123406 cents= "<<"$"<<d<<"."<<c<<"\n";

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    io formatting

    that is because you are working with long integers and float variables...
    zMan

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Change your cout statement to this:

    cout<<setprecision(2)<<"123406 cents= "<<"$"<<d<<".";
    if(c < 10)
    cout<<"0";
    cout<<c<<"\n";

    You must print the leading 0s yourself.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    have a look at the second last post on this page
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    cout.setf(ios::fixed,ios::floatfield);
    cout.width(2);
    cout << setprecision(2) << "123406 cents= " << "$" << d << ".";
    cout.width(2);
    cout << setfill('0') << c << endl;

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    With respect to Cshot and Swoopy, an alternative (not better, just an alternative) method:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #define cents_per_dollars 100 //cents in a dollar
    
    int main (void)
    {
    long cents;
    long d, c;
    int fillFlag = 0;
    cents = 123406;
    
    d = cents / cents_per_dollars;
    c = cents % cents_per_dollars;
    
    if (c < 10)
       fillFlag = 1;
    
    std::cout << cents << " cents = " << "$" << d << ".";
    
    if (fillFlag)
       std::cout << std::setw(2) << std::setfill('0') << c << '\n';
    else
       std::cout << c << '\n';
    
    getch();
    return 0;
    }
    The only real advantage? You can change the value of 'cents' without the hardcode restriction in the 'std::cout' statement.

    (Please...no, stop...you're all far too kind... )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  4. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21