Thread: significant numbers

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    significant numbers

    Hello again,
    I've been searching a lot of FAQs and doing a lot of searching but I cant seem to find this one answer (maybe no one else cares )...

    The question is: how can I choose how many significant numbers are printed to screen? Meaning, I want to show .50 but my program displays .5 only and also, I want to display 2.00 but I only get 2...any ideas or good shoves in the right direction? Thanks a lot!
    -Chap

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    oh and sorry to be a pain...but one more question...is this unnecessary? In the following, ch is an int

    Code:
    while ((ch = getch()) != ESC)
    	{
    		if (static_cast<char>(ch) == 'p')
    		{
    			toll.payingCar();
    			cout << "A paying car!\n";
    		}
    what I'm asking about is the static_casting of ch to type char. The program works both ways, I was just wondering why it would work both ways and if I should not waste my time with the static_cast

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Chaplin27
    Hello again,
    I've been searching a lot of FAQs and doing a lot of searching but I cant seem to find this one answer (maybe no one else cares )...

    The question is: how can I choose how many significant numbers are printed to screen? Meaning, I want to show .50 but my program displays .5 only and also, I want to display 2.00 but I only get 2...any ideas or good shoves in the right direction? Thanks a lot!
    -Chap
    I assume this is what you want:
    Code:
           #include<iostream>
    	#include <iomanip>
    	using namespace std;
    
    	int main()
    	{
                    double x = 12.123456789;
    		cout<<setprecision(10);
    		cout<<x;
    		system("pause");
    		return 0; // success return
    	}
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    thanks for the answer...but it doesnt seem to have changed anything...I think the way setprecision works (tell me if I'm wrong) is if you have an overflow of numbers (.1234) but you want to chop it down to what you need (.12)...what if you need to add numbers that technically dont exist? in my program, incrementing the total dollar amount by .50 will generate a simple 1, I dont think setprecision will add a decimal and 2 zeros.

    Maybe it's going to simply take some math? Only one way to find out...

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Chaplin27
    oh and sorry to be a pain...but one more question...is this unnecessary? In the following, ch is an int

    Code:
    while ((ch = getch()) != ESC)
    	{
    		if (static_cast<char>(ch) == 'p')
    		{
    			toll.payingCar();
    			cout << "A paying car!\n";
    		}
    what I'm asking about is the static_casting of ch to type char. The program works both ways, I was just wondering why it would work both ways and if I should not waste my time with the static_cast

    1. Since getch() is not a standard C (or C++) library function, I can't comment on what's necessary or unnecessary.

    If you were using getchar(), however, the cast would be necessary to print it as a char since ch should be an int (so that you can test for the return value of EOF). Of course getchar() can't read an ESC, so it won't do you much good, but I just thought you might want to know why you might see something like that in a program

    2.

    Use fixed and setprecision to specify the exact number of digits after the decimal
    Code:
    #include<iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      double x = 12.5;
      double y = 12;
      double z = 12.344;
      cout << "x = " << fixed << setprecision(2) << x << endl;
      cout << "y = " << fixed << setprecision(2) << y << endl;
      cout << "z = " << fixed << setprecision(2) << z << endl;
      return 0; // success return
    }
    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    dave...greatest answer ever...thanks!and Micko thanks yet again for the help
    -Chap

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    one more thing...if I have an answer of 29.8, how can I convert that to 30?

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Chaplin27
    one more thing...if I have an answer of 29.8, how can I convert that to 30?
    If you have the function round() in your math library (part of C99 Standard library --- gcc has it) use it.

    If not then you can use standard library functions ceil() and floor() (prototyped in <cmath>) as follows:
    suppose we have this

    Code:
    double x, xrounded;
    Then for positive numbers you can use this:

    Code:
    xrounded = floor(x + 0.5);
    For negative numbers:

    Code:
    xrounded = ceil(x - 0.5);
    Regards,

    Dave
    Last edited by Dave Evans; 04-04-2005 at 02:22 PM.

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Yes, Dave Evans is absolutely right, I was in a hurry (going to swimming pool) and didn't fully understand your question.
    When ios::fixed is set, floatingpoint values are printed using decimal notation.
    For example
    Code:
            #include<iostream>
    	#include <iomanip>
    
    	int main()
    	{
                    double x = 12.0;
    		cout<<fixed<<setprecision(7)<<x;
    		return 0; // success return
    	}
    wil display 12.0000000.
    Thanks Dave for correcting me!
    Cheers!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    or seting ios flags... which can keep the settings for a whole program.

    Code:
    cout<< setiosflags( ios::fixed | ios::showpoint);
    example:

    Code:
    #include <iomanip>
    #include <iostream>
    using namespace std;
    
    main()
    {
       cout << setiosflags( ios::fixed | ios::showpoint );
       double a = 2.5555;
       float b = 2;
       
       cout<< setprecision(2);
       cout<< a << endl << b << endl;
       
       return 0;
    }
    
    /*
    OUTPUT:
    2.56
    2.00
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM