double conversion

This is a discussion on double conversion within the C++ Programming forums, part of the General Programming Boards category; How do I convert double 1.00 into int 1?...

  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
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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, 12: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

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