calculate numbers

This is a discussion on calculate numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, How can i calculate numbers and round off the decimal point to 2 decimal places? for example i have ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    calculate numbers

    Hi,

    How can i calculate numbers and round off the decimal point to 2 decimal places?

    for example i have 2 numbers 5 and 3

    the result of 5 divide by 3 is 1.666666666

    i want it to return me only 1.66 or 1.67 for example..

    how could i go about it? and for my numbers what variable type should they be? double? float? i've tried all but i couldn't get the result i want.

    Anyone can advise?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > i want it to return me only 1.66 or 1.67 for example.
    Is that what you want to print, in which case it would be something like
    Code:
        double a = 5.0 / 3.0;
        cout << setprecision(3) << a << endl;
    If you really want to store the rounded results, then you need to understand that floats are approximations. So even if you round the result to 1.67, this may in fact be represented as 1.66999999 or 1.67000001 as being the nearest representable value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Salem View Post
    > i want it to return me only 1.66 or 1.67 for example.
    Is that what you want to print, in which case it would be something like
    Code:
        double a = 5.0 / 3.0;
        cout << setprecision(3) << a << endl;
    If you really want to store the rounded results, then you need to understand that floats are approximations. So even if you round the result to 1.67, this may in fact be represented as 1.66999999 or 1.67000001 as being the nearest representable value.
    For printing, using only setprecision is not enough:
    Code:
        double a = 50.0 / 3.0;
        cout << setprecision(3);
        cout << a << endl;
    prints 16.7.

    You'll need to use the "fixed" manipulator also, and specify the decimal places you want after the decimal point:
    Code:
        double a = 50.0 / 3.0;
        cout << fixed << setprecision(2);
        cout << a << endl;
    (This prints 16.67)

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    thanks both of u! the solution works wonders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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