Thread: simple problem

  1. #1
    none
    Guest

    simple problem

    how do you make an integer accept a decimal value?

    I know i knew this, but now...and it's not in the faq

    somebody please help

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Use a float, not an int?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    how do you make an integer accept a decimal value?
    use a double
    eg;
    double n = 6.0

    U R right, pretty simple, my friend
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    u could cast it.

    int variable;
    int final;

    final = double(variable);

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Ride -or- Die
    u could cast it.

    int variable;
    int final;

    final = double(variable);
    errr... no. final is an int and will only ever hold an int value. The use of the implied cast is also incorrect, you meant this:
    >final = (double)variable;
    ... but it wouldn't store the digits to the right of the decimal point, if there were any. Of course, there can't be any though, as variable is an int as well.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    none
    Guest
    wow....

    thanks for all the alternatives, but i think i'll stick with "float".

    Sorry to trouble you with such a simple question.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Hammer

    >>final = double(variable);
    The use of the implied cast is also incorrect, you meant this:
    final = (double)variable;
    I don't see anything wrong with double(variable).
    It is C++-style casts.

    Nevertheless, the cast is useless.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I don't think we can cast with: double(variable).
    it's just like:
    Code:
    int x=5;
    double y=9;
    
    x=y;
    isn't it?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>It is C++-style casts.
    Oops, you're right, I didn't spot that

    >>Nevertheless, the cast is useless.
    That was my main point too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM