std::setprecision

Discussion in 'C++' started by tarmat, Nov 5, 2003.

  1. tarmat

    tarmat Guest

    I'm trying to create a function that will turn a float into a
    std::string that always shows the number to two decimal places. I have
    the following but it doesn't give the desired output:


    inline std::string ftos(float f)
    {
    std::eek:stringstream buffer;

    buffer << std::setprecision(2) << f;

    return buffer.str();
    }

    for example

    ftos (34.22) gives "34"
    ftos(3.123) gives "3.1"

    how do I create a function that will simply chop of all the digits
    after the second digit from the decimal point?

    thanks
    tarmat, Nov 5, 2003
    #1
    1. Advertising

  2. tarmat

    tom_usenet Guest

    On Wed, 05 Nov 2003 20:04:40 +0530, tarmat <>
    wrote:

    >
    >inline std::string ftos(float f)
    >{
    > std::eek:stringstream buffer;
    >
    > buffer << std::setprecision(2) << f;


    buffer << std::fixed << std::setprecision(2) << f;

    Tom
    tom_usenet, Nov 5, 2003
    #2
    1. Advertising

  3. tarmat

    Fraser Ross Guest

    I don't see what happened. Isn't normal notation the default? How many
    notations is there?

    Fraser.


    > >inline std::string ftos(float f)
    > >{
    > > std::eek:stringstream buffer;
    > >
    > > buffer << std::setprecision(2) << f;

    >
    > buffer << std::fixed << std::setprecision(2) << f;
    >
    > Tom



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.532 / Virus Database: 326 - Release Date: 27/10/2003
    Fraser Ross, Nov 5, 2003
    #3
  4. tarmat

    Fraser Ross Guest

    I see those numbers did not need a E in them. Scientific notation is the
    default.

    Fraser.

    > I don't see what happened. Isn't normal notation the default? How many
    > notations is there?
    >
    > Fraser.
    >
    >
    > > >inline std::string ftos(float f)
    > > >{
    > > > std::eek:stringstream buffer;
    > > >
    > > > buffer << std::setprecision(2) << f;

    > >
    > > buffer << std::fixed << std::setprecision(2) << f;
    > >
    > > Tom
    Fraser Ross, Nov 5, 2003
    #4
  5. tarmat

    tom_usenet Guest

    On Wed, 5 Nov 2003 17:08:15 -0000, "Fraser Ross"
    <fraserATmembers.v21.co.unitedkingdom> wrote:

    >I don't see what happened. Isn't normal notation the default? How many
    >notations is there?


    3 main ones I think: normal, scientific and fixed precision.

    Tom
    tom_usenet, Nov 5, 2003
    #5
  6. tarmat

    Jerry Coffin Guest

    In article <>, "Fraser Ross"
    <fraserATmembers.v21.co.unitedkingdom> says...
    > I don't see what happened. Isn't normal notation the default? How many
    > notations is there?


    In C there are three conversions: %e, %f and %g. By default, ostreams
    use the equivalent of %g, which automatically chooses between %e and %f.
    For ostreams, these are equivalent to scientific and fixed respectively.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Nov 9, 2003
    #6
    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. Woodster
    Replies:
    2
    Views:
    2,791
    Senthilvel Samatharman
    Nov 14, 2003
  2. Gary Wessle

    setprecision in output

    Gary Wessle, Aug 8, 2006, in forum: C++
    Replies:
    1
    Views:
    388
    Gary Wessle
    Aug 8, 2006
  3. Wing

    setprecision problem

    Wing, Oct 12, 2006, in forum: C++
    Replies:
    2
    Views:
    435
    Spoon
    Oct 12, 2006
  4. Anjo Gasa

    Unexpected setprecision behavior

    Anjo Gasa, Feb 1, 2007, in forum: C++
    Replies:
    3
    Views:
    384
  5. mathieu
    Replies:
    1
    Views:
    1,770
    James Kanze
    Mar 11, 2009
Loading...

Share This Page