Round a double to 3 decimalplaces

This is a discussion on Round a double to 3 decimalplaces within the C++ Programming forums, part of the General Programming Boards category; I am trying to round a double to 3 decimalplaces like this example. The output will be 12.345 but when ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Round a double to 3 decimalplaces

    I am trying to round a double to 3 decimalplaces like this example.
    The output will be 12.345 but when I am compiling the program it says:
    ´floor´: identifier not found.

    Perheps I have to #include a class for this and if, which one can be used for this ?

    Code:
    double d = 12.3446;
    d = floor(d * 1000.0 + 0.5 ) / 1000.0;

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    I'm sure you have to use #include <iomanip>. In the cout statement when displaying the output, you should have something like this (I'm using dev- C++):
    cout << fixed << showpoint << setprecision(3) << d;
    Last edited by getName(C-Dub); 01-20-2008 at 05:03 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or if you want the value itself to be changed, you can include <cmath>.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    The <cmath> did work for me... thanks a lot...

    ..........
    coding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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