How to convert an integer to double?

This is a discussion on How to convert an integer to double? within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm beginner, Can please someone tell me how to convert an integer into double. Another general question: Can ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    How to convert an integer to double?

    Hey guys,

    I'm beginner, Can please someone tell me how to convert an integer into double.

    Another general question: Can someone give me a link where i can find all the useful methods (library methods). For example, for java we can find all the different classes and methods on sun site. I found out that the online tutorials are not that helpful.

    I would really aperciate the help . Please someone? Thanks in advance!!

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Assume the following decleration for each example:
    Code:
    int number;
    Here are a couple of ways...
    Code:
    double(number);
    Code:
    static_cast<double>(number);

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Thanks Josh ... It worked

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by JoshR
    Code:
    double(number);
    You mean of course
    Code:
    (double)number;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    double(number) also works - it uses a constructor, from what I understand.
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    No need for a cast at all, the conversion is implicit. Just assign the int to the double.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by laserlight
    double(number) also works - it uses a constructor, from what I understand.
    In C, only (double)value works, but the double(value) syntax became legal in C++ to allow primitive casting to appear like a function call. (No, it's not a constructor.)

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Daved
    No need for a cast at all, the conversion is implicit. Just assign the int to the double.
    Who said he was assigning it to something? maybe he wanted to do calculations with it and print it.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I was originally taught to use double(number), then I found out about other ways and now either use static_cast<double>(number) or (double)number...

    and for your reference: http://www.cppreference.com

    be warned though: there are alot of non-standard functions floating around in the world of C/C++ (mostly because it's not platform-independent, like Java), and that site only provies standard methods (ones should work on any (popular) platform).
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM

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