number of decimal places

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

  1. shez

    shez Guest

    Hi,

    Is there any way to output a double value with different number of
    decimal places (apart from using 'sprintf'). I looked up the reference
    for C++ streams and found the 'setprecision' manipulator, but this sets
    the number of digits before and after the decimal point. I can't seem
    to find any manipulator for setting the number of digits after the
    decimal point.

    Thanks,
    -shez-
     
    shez, Jan 20, 2005
    #1
    1. Advertising

  2. shez

    shez Guest

    Sorry guys, i've found the answer. Pls ignore.

    Sorry,
    -shez-
     
    shez, Jan 20, 2005
    #2
    1. Advertising

  3. On 19 Jan 2005 19:11:42 -0800, shez <> wrote:

    > Sorry guys, i've found the answer. Pls ignore.


    you would not need to be sorry, if you were so kind to post the answer you
    have found - perhaps others are interested, too.

    ps: ignoring, as you suggest, is not possible, because to do so, anybody
    must read your original posting, plus your answer to it...

    --
    have a nice day
    ulrich
     
    Ulrich Achleitner, Jan 20, 2005
    #3
  4. shez

    shez Guest

    > you would not need to be sorry, if you were so kind to post the
    answer you
    > have found - perhaps others are interested, too.


    You can use the 'std::fixed' manipulator before setprecision, like
    this:

    std::cout << std::fixed << std::setprecision(2) << 1.2345678 <<
    std::endl;

    This should print to two decimal places. Thanks, -shez-
     
    shez, Jan 20, 2005
    #4
  5. shez

    saurabh.unercat

    Joined:
    Feb 19, 2011
    Messages:
    1
    the above codes work for the newr compilers...if any of you are luking fr things that work with <xyz.h> files only...use the setf as follows


    cout.setf(ios::fixed);
    cout<<setprecision(4)<<number;


    ..//this prints exactly 4 digits after a decimal point :D
     
    saurabh.unercat, Feb 19, 2011
    #5
    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. Gizmo
    Replies:
    1
    Views:
    3,605
    Jakob Bieling
    Aug 31, 2003
  2. Gaijinco
    Replies:
    29
    Views:
    2,936
    Anonymous 7843
    Sep 28, 2005
  3. Gaijinco
    Replies:
    27
    Views:
    1,091
    Anonymous 7843
    Sep 28, 2005
  4. harryos
    Replies:
    4
    Views:
    1,658
  5. Dr John Stockton

    Rounding a Number to a String with N decimal places

    Dr John Stockton, Aug 15, 2004, in forum: Javascript
    Replies:
    1
    Views:
    103
    G Roydor
    Aug 15, 2004
Loading...

Share This Page