Using a double

This is a discussion on Using a double within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to keep a number with a 5 digit percision. for some reason when I keep the number ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    Lightbulb Using a double

    Hi,
    I need to keep a number with a 5 digit percision. for some reason when I keep the number 99.99999 in a double, the actual number kept is 99.98999995.
    I want to round the number to its 5th precision point.
    Any ideas how to do that?
    Thanks,
    RMOK

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    the actual number kept is 99.98999995.
    It is usual that the float (or double) contains values that differ from the value it should have contained. Look here

    want to round the number to its 5th precision point.
    Maybe you could do something like:


    Code:
    double ADouble=99.24689657;
    double RoundResult=round(ADouble*(double)1e3)/(double)1e3;
    Last edited by MathFan; 06-06-2005 at 06:22 AM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Won't setprecision() help?
    cout<<setprecesion(5)<<float_variable; //Is'nt this right?

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    cout<<setprecesion(5)<<float_variable;
    Well, that is right, but that is if you do not want to change variable's value. In your example you set precision of OUTPUT values pushed through iostream. And that won't change the variable itself.
    Last edited by MathFan; 06-06-2005 at 06:24 AM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

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. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 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