stringstream

Discussion in 'C++' started by Flzw, Jan 20, 2005.

  1. Flzw

    Flzw Guest

    I can't seem to find how to set fixed 2 digits after the dot format to float
    and double numbers, I get "<< operator is ambiguous " if I try using
    setprecision and ios::fixed...any help ?
    Flzw, Jan 20, 2005
    #1
    1. Advertising

  2. Flzw wrote:
    > I can't seem to find how to set fixed 2 digits after the dot format to float
    > and double numbers, I get "<< operator is ambiguous " if I try using
    > setprecision and ios::fixed...any help ?


    You're on the right track. What's the code you have a problem with?

    #include <iostream>
    #include <iomanip>

    int main()
    {
    float pi = 3.1415926f;
    std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
    std::cout << std::fixed << std::setprecision(4) << pi << std::endl;
    }

    Prints
    3.14
    3.1416
    Victor Bazarov, Jan 20, 2005
    #2
    1. Advertising

  3. Flzw

    Flzw Guest

    >> I can't seem to find how to set fixed 2 digits after the dot format to
    >> float and double numbers, I get "<< operator is ambiguous " if I try
    >> using setprecision and ios::fixed...any help ?

    >
    > You're on the right track. What's the code you have a problem with?
    >
    > #include <iostream>
    > #include <iomanip>
    >
    > int main()
    > {
    > float pi = 3.1415926f;
    > std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
    > std::cout << std::fixed << std::setprecision(4) << pi << std::endl;
    > }
    >
    > Prints
    > 3.14
    > 3.1416


    Well yes I know how to do that with iostream / cin / cout but I want
    something similar with a ostringstream, I was'nt explicit enough, sorry ; )
    There is no setf() member function for an ostreamstring either....
    Flzw, Jan 20, 2005
    #3
  4. Flzw wrote:
    >>>I can't seem to find how to set fixed 2 digits after the dot format to
    >>>float and double numbers, I get "<< operator is ambiguous " if I try
    >>>using setprecision and ios::fixed...any help ?

    >>
    >>You're on the right track. What's the code you have a problem with?
    >>
    >>#include <iostream>
    >>#include <iomanip>
    >>
    >>int main()
    >>{
    >> float pi = 3.1415926f;
    >> std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
    >> std::cout << std::fixed << std::setprecision(4) << pi << std::endl;
    >>}
    >>
    >>Prints
    >>3.14
    >>3.1416

    >
    >
    > Well yes I know how to do that with iostream / cin / cout but I want
    > something similar with a ostringstream, I was'nt explicit enough, sorry ; )
    > There is no setf() member function for an ostreamstring either....


    What are you talking about???

    #include <iostream>
    #include <iomanip>
    #include <sstream>

    int main()
    {
    float pi = 3.1415926f;
    std::eek:stringstream os;
    os << std::fixed << std::setprecision(2) << pi << std::endl;
    os << std::fixed << std::setprecision(4) << pi << std::endl;
    std::cout << os.str();
    }

    Prints the same thing. That's the beauty of streams.
    Victor Bazarov, Jan 20, 2005
    #4
    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. Marc Schellens

    stringstream

    Marc Schellens, Jul 1, 2003, in forum: C++
    Replies:
    3
    Views:
    529
  2. minjie
    Replies:
    1
    Views:
    658
    Victor Bazarov
    Jul 15, 2003
  3. Bill Beacom

    stringstream problem

    Bill Beacom, Sep 15, 2003, in forum: C++
    Replies:
    2
    Views:
    463
    Peter Kragh
    Sep 15, 2003
  4. Ellarco
    Replies:
    5
    Views:
    4,529
    Ellarco
    Sep 24, 2003
  5. Medi Montaseri

    How to shrink a stringstream

    Medi Montaseri, Sep 25, 2003, in forum: C++
    Replies:
    2
    Views:
    377
    Medi Montaseri
    Sep 26, 2003
Loading...

Share This Page