Thread: double conversion

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    double conversion

    How do I convert double 1.00 into int 1?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    I'm not sure if this is correct, but why not use static_cast<int>(1.00). I'm not sure if it's written correctly, but it changes the double into an integer.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    double dbl = 1.00;
    int doubleToInt = (int)dbl;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, the static_cast as suggested by getName(C-Dub) is preferred to the C-style cast syntax as in mikeman118's example.

    It is probably safe to cast 1.00 to 1, but note that more generally it may be possible that what looks like an integer when printed may be less than that integral value, thus the truncation when casting to int may result in an integer smaller than expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM