Help with outputting 2 decimal places...

Discussion in 'C++' started by David Casey, Feb 20, 2004.

  1. David Casey

    David Casey Guest

    I'm trying to output 2 decimal places to the screen even when the last
    digit would be 0. Here is what I have now:

    #include <iostream.h>
    #include <iomanip.h>

    double custname=0;

    void main()
    {
    custname=12.5;
    cout << setprecision(4) << custname;
    }

    When I set custname to a value like 12.59 it prints 12.59. But the above
    only prints 12.5. I've searched and tried several different ways on Google
    and nothing seems to work.

    Just so you know, I'm just starting out with C++. Thanks in advance for
    any help anyone can provide.

    Dave
    --
    You can talk about us, but you can't talk without us!
    US Army Signal Corps!!
    http://www.geocities.com/davidcasey98

    Remove "IH8SPAM" to reply!
     
    David Casey, Feb 20, 2004
    #1
    1. Advertising

  2. David Casey

    red floyd Guest

    David Casey wrote:

    > I'm trying to output 2 decimal places to the screen even when the last
    > digit would be 0. Here is what I have now:
    >
    > #include <iostream.h>
    > #include <iomanip.h>
    >
    > double custname=0;
    >
    > void main()
    > {
    > custname=12.5;
    > cout << setprecision(4) << custname;
    > }
    >
    > When I set custname to a value like 12.59 it prints 12.59. But the above
    > only prints 12.5. I've searched and tried several different ways on Google
    > and nothing seems to work.
    >
    > Just so you know, I'm just starting out with C++. Thanks in advance for
    > any help anyone can provide.
    >
    > Dave


    1. You're using old headers.
    2. use showpoint or fixed
    3. Main returns int

    #include <iostream>
    #include <iomanip>

    using namespace std;

    double custname = 0;
    int main()
    {
    custname = 12.5;
    cout << setprecision(4) << showpoint << custname << endl;
    cout << setprecision(4) << fixed << custname << endl;
    return 0;
    }


    generates:

    12.50
    12.5000


    }
     
    red floyd, Feb 20, 2004
    #2
    1. Advertising

  3. David Casey

    David Casey Guest

    On Fri, 20 Feb 2004 00:41:57 GMT, red floyd wrote in comp.lang.c++:

    > 1. You're using old headers.


    Yes, fixed that now. :)

    > 2. use showpoint or fixed


    I hadn't heard of those commands. The teacher was telling us to just put
    cout.precision(2) right inside of main, but that wasn't working.

    > 3. Main returns int


    Yes, I know but the teacher hasn't "shown" us that part yet. I know a
    little more than the teacher is telling us because it's just a programming
    logic class and I've done a little reading on my own regarding C++. I
    didn't want to use stuff the teacher hasn't shown us in class.

    > #include <iostream>
    > #include <iomanip>
    >
    > using namespace std;
    >
    > double custname = 0;
    > int main()
    > {
    > custname = 12.5;
    > cout << setprecision(4) << showpoint << custname << endl;
    > cout << setprecision(4) << fixed << custname << endl;
    > return 0;
    > }
    >
    >
    > generates:
    >
    > 12.50
    > 12.5000
    >
    >
    > }


    I just gave this a shot and it worked good:

    #include <iostream>
    #include <iomanip>

    using namespace std;

    float custname=0;
    float roomnum=0;

    void main()
    {
    cout.setf(ios_base::fixed, ios_base::floatfield);
    cout.precision(2);
    cout << custname << endl;
    cout << roomnum << endl;
    }

    It also seems to toggle on the 2 decimal place precision which is what I
    was looking for and I think the teacher meant but I don't know why he
    didn't tell us about the cout.setf line. I also figured out that without
    the using namespace std; line, it doesn't work either and that's also
    something the teacher hasn't told us to use so again, I wasn't going to go
    over what he was already showing us. However, in this case I think I'll
    just use it (like I have a choice).

    Thanks for the help, though. It gives me another choice to use. :)

    Dave
    --
    You can talk about us, but you can't talk without us!
    US Army Signal Corps!!
    http://www.geocities.com/davidcasey98

    Remove "IH8SPAM" to reply!
     
    David Casey, Feb 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,617
    Eoin Mcloughlin
    Feb 13, 2004
  2. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Curt_C [MVP]
    May 18, 2004
  3. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    544
    Curt_C [MVP]
    May 18, 2004
  4. Harry

    Remove Decimal Places

    Harry, Jun 3, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    8,966
    VB Programmer
    Jun 3, 2004
  5. Bharat Bhushan

    Decimal Places

    Bharat Bhushan, Aug 8, 2003, in forum: Java
    Replies:
    2
    Views:
    514
    Lee Weiner
    Aug 8, 2003
Loading...

Share This Page