allow decimals

This is a discussion on allow decimals within the C++ Programming forums, part of the General Programming Boards category; when I use "int" it doesnt allow decimals. What allows decimals?. Also lets say I make an integer called x, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    allow decimals

    when I use "int" it doesnt allow decimals. What allows decimals?. Also lets say I make an integer called x, a and b, and I want x to be a*b what should I do?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    In C++ the default type for decimal numbers is double, so use double instead of int.

    To assign the value of a*b to x, you do just that, assign a*b to x. If you know what the operator for assignment is in C++, that will help.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >when I use "int" it doesnt allow decimals.
    That's because the int type stores an integer, which doesn't support precision. You want a real number, or floating-point, supported by the float, double, and long double types.

    >Also lets say I make an integer called x, a and b, and I want x to be a*b what should I do?
    First you should get a book on C++. Then you should read it.
    My best code is written with the delete key.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    If you don't know a float or double in C++, you really need to take a course, look at this site's tutorials, or buy a book. And to answer your question about multiplying, use these lines of code:
    Code:
    double a = 1.5;
    double b = 3.45;
    double x = a*b;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  2. Getting enough decimals
    By Drogin in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2005, 09:37 PM
  3. multiple decimals = problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:32 AM
  4. Decimals To Binary
    By kas2002 in forum C++ Programming
    Replies: 8
    Last Post: 06-08-2002, 11:12 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

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