setprecision woes

This is a discussion on setprecision woes within the C++ Programming forums, part of the General Programming Boards category; Hello, this is my first post here...please bear with me, I'm an amateur at this... I want to write a ...

  1. #1
    foo
    foo is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    Post setprecision woes

    Hello, this is my first post here...please bear with me, I'm an amateur at this...

    I want to write a program that can display any nonnegative floating point value as a decimal number with exactly two digits to the right of the decimal, because we are dealing with monetary quantities.

    This is pretty straightforward, so I have something to the effect of
    Code:
    	float pay;
    	int precision;
    
    int main() {
    
    ...
    
    	cout<<"Enter the pay\n";
    	cin>>pay;
    
    	precision=floor(log10(pay))+3;
    
    	cout<<setprecision(precision)<<"\n\nThe pay is "<<pay<<"\n"
    
    ... }
    This works in most but not all cases, namely when pay < .01. Even when setprecision is 0 or below it still displays one significant digit (i.e. 0.003). I could get around this by placing a check to display the quantity as 0.00 when precision < 1, I suppose, but then a quantity like 0.009 won't be rounded up to 0.01. Maybe multiply by 100 and round to the nearest integer, then divide by 100 again?

    As well, when an integer quantity like 123 is inputted, it is redisplayed as an integer though the value for setprecision will be what I want it to be (5 in this case). I could tack on ".00" when pay is an integer, but that doesn't account for situations where 123.006 can be displayed as 123.01 but 123.004 -- a non integer value -- will be regurgitated as simply 123. Maybe I should follow my own suggestion again, multiply/round/divide with 100.

    I have a feeling I'm making this a lot harder than it needs to be or missing something obvious. Maybe I should forgo setprecision altogether, and display the integer quantity and then calculate/display the rounded quantity to the right of the decimal.

    Oh well, even if no one responds to this thread, typing it out helped me think the problem through

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could try this, if all the formatting in the whole program is the same.

    Code:
    #include <iomanip>
    
    int main()
    {
        int number;
        std::cout << setprecision(2);
        std::cout.setf(ios::showpoint | ios::fixed);
    
        ...
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    alpha is right. ios::fixed is the key to your problem.

    setprecision() has "default" settings which are fine as long as you understand how they function. (It doesn't discriminate between the mantissa and decimal value.)

    By "invoking" ios::fixed, you can use setprecision() to fix the number of decimal places that you need.

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

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    floats

    Ok I use an oldschool compiler (borland turbo c++ from 19-digity-2) but here is my solution. also i dont know what alpha used (in terms of std - could some1 explain what it is thx) so here:

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>
    
    int main()
    {
        float pay;
    
        clrscr(); // or void clrscr(); when i try on dev c++
        cout.setf(ios::showpoint);
        cout.setf(ios::fixed);  // i think this is the same thing he did
    
        cout << "\nPay: ";
        cin >> pay;
    
        cout  << endl << setprecision(2) << pay;
    }
    thats just my solution hope it helps =\
    ~fin

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Ok I use an oldschool compiler (borland turbo c++ from 19-digity-2)
    Borland's 5.5 compiler is free for download and supports all the good stuff from ISO C++ :-)
    *Cela*

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    Borland's 5.5 compiler is free for download and supports all the good stuff from ISO C++ :-)
    Really? Wow, thanks alot is it at borland.com or something, ive used this old software because its what we use in class. I usually just play around because the class is WAY too easy so when it comes time for extra credit (sorting array's, tic-tac-toe game) i shine =). BTW could anyone think of a decently challenging project that involves pretty much basic knowledge of pointers and requires good knowledge of classes, good function use, ect. Ive made a base converter, tic-tac-toe game, and some others. Also i wan't to try and make a chess program, and i know recursive functions, so where would be a good place to start.

    thx
    ~fin

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>is it at borland.com or something
    Second on the list here. :-)

    >>BTW could anyone think of a decently challenging project that involves pretty much basic knowledge of
    >>pointers and requires good knowledge of classes, good function use, ect.
    How about a little interpreted language? Real simple stuff like
    Code:
    ~ An interpreted program
    ~ Interpreter written in C++
    {
      scalar(a,b,c) := (1,2,3).
      c := a + b + c.
      write CONSOLE c.
    }
    *Cela*

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    the std:: stuff is new standard, the iostream.h with the .h ending will give you backwards warnings with newer compilers. so you could do one of the following:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    }
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    //continue with what needs to be used
    
    int main() {
    }
    Code:
    #include <iostream>
    
    int main() {
        std::cout << std::endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setprecision()
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2006, 08:38 AM
  2. setprecision??
    By rachael033 in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2006, 01:33 AM
  3. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 01:26 PM
  4. MD2 woes
    By psychopath in forum Game Programming
    Replies: 9
    Last Post: 07-02-2005, 07:46 PM
  5. setprecision()
    By OnionKnight in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2005, 08:08 PM

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