How to convert double

This is a discussion on How to convert double within the C Programming forums, part of the General Programming Boards category; I have array of double arr1[10] how can i save it to array of int arr2[10], I main how to ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    How to convert double

    I have array of double arr1[10] how can i save it to array of int
    arr2[10], I main how to get integer part from the double and ignore the factionary part.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    When a double value is assigned to an integer variable, the fractional part automatically gets truncated .i.e., only the integer part goes into the variable. (See example below)

    Code:
    double ar1[2];
    int ar2[2];
    
    ar1[0]=5.01929384574;
    ar1[1]=1234.9818236;
    Implicit Type Casting

    Code:
    ar2[0]=ar1[0];
    ar2[1]=ar1[1];
    Explicit Type Casting

    Code:
    ar2[0]=(int)ar1[0];
    ar2[1]=(int)ar1[1];
    Both works.
    ----------------------------------------------------------------------------
    This is the answer if i had understood the question correctly

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    Thank you, I will try it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert from double to string?
    By beon in forum C Programming
    Replies: 10
    Last Post: 10-19-2007, 03:45 AM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 04:14 AM
  5. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM

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