setprecision()

This is a discussion on setprecision() within the C++ Programming forums, part of the General Programming Boards category; if u want to setprecision you need to do it like this: cout<<setprecision(3)<<.......... is there a way that you only ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    70

    setprecision()

    if u want to setprecision you need to do it like this:

    cout<<setprecision(3)<<..........

    is there a way that you only need to say it once so it stay in memory?????

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Use the stream member precision().

    Code:
    std::cout.precision(3);
    EDIT: You can find other interesting iostream function members here:
    http://www.cplusplus.com/ref/iostream/ostream/
    and
    http://www.cppreference.com/cppio/index.html
    Last edited by Mario F.; 11-25-2006 at 08:01 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    Code:
           if(d==0)std::setprecision(0);
           if(d==1)std::setprecision(1);
           if(d==2)std::setprecision(2);
           if(d==3)std::setprecision(3);
           if(d==4)std::setprecision(4);
           if(d==5)std::setprecision(5);
           if(d==6)std::setprecision(6);
           if(d==7)std::setprecision(7);
           if(d==8)std::setprecision(8);
           if(d==9)std::setprecision(6);
    still not doing it if i do 1.225*1.252245 with 1precision i still not get that precision:s

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I don't think you understood:

    Code:
    double value = 23.4567;
    
    std::cout.precision(3);
    
    std::cout << value << std::endl; // outputs 23.4
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    ow , i missread it

    but now it works

    but , i want that the setprecision only is true behind the , so:
    precision(1)
    99.11*1=99.1

    how do you that?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    The number of digits of an integer can be calculated by adding one to the common logarithm of that number. In C++ the common logarithm is given by log10() defined in <cmath>.

    So... one way is:

    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    int main {
        double value = 99.11;
    
        int numdigits = log10(value) + 1;  // returns 2. The number of whole digits in value
    
        int myprecision = numdigits + 1;  // You want one more for one decimal place
    
        cout.precision(myprecision);
    
        cout << value << endl;  // outputs 99.1
    }
    EDIT: removed an std:: ... force of habit.
    Last edited by Mario F.; 11-25-2006 at 09:54 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setprecision??
    By rachael033 in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2006, 02:33 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  3. setprecision()
    By OnionKnight in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2005, 09:08 PM
  4. Setw() and setprecision?
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 09:20 AM
  5. setprecision() Help
    By frgmstr in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 03:24 PM

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