type casting

This is a discussion on type casting within the C++ Programming forums, part of the General Programming Boards category; Hello everybody who reads this.. I've q question about typecasting. If you cast an double or float to an int, ...

  1. #1
    Unregistered
    Guest

    type casting

    Hello everybody who reads this..

    I've q question about typecasting. If you cast an double or float to an int, will this double or float round up or round? And if the double is negative will that make any difference?

    Thanks for your attention on this..

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    It will truncate the value to an integer value, negative or positive.

  3. #3
    Unregistered
    Guest

    how exactyle does double's be round up/down

    But if an double has the value 2,6. And this double will be casted to an integer, has this integer the value 2 or 4?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Do you mean 2.6 ? Because double cannot have 2,6.

  5. #5
    Unregistered
    Guest

    none

    eh sorry i'm dutch and here we use a , instead of . so it must be a double with the value of 2.6

  6. #6
    Unregistered
    Guest

    none

    eh sorry the question must be:

    But if an double has the value 2,6. And this double will be casted to an integer, has this integer the value 2 or 3?

    thanks for your attention and sorry for my poor english...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    16
    it should have a value of 2

  8. #8
    Unregistered
    Guest

    and -1.5?

    and -1.5?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    16
    are you doing homework???

    here is an example of a program and some output:

    #include <iostream.h>
    #include <iomanip.h>

    int main ()
    {
    int a;
    float b;

    cout << setprecision( 2 );

    cout << "Enter a decimal number: ";
    cin >> b;
    a = b;
    cout << "The output after putting it into an int is: " << a
    << endl << endl;

    return 0;
    }


    Warning message (program still works):
    ex1.cpp: In function `int main()':
    ex1.cpp:21: warning: assignment to `int' from `float'

    Output:
    1) Enter a decimal number: 4.322
    The output after putting it into an int is: 4
    2) Enter a decimal number: -1.5
    The output after putting it into an int is: -1

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    16
    in the program this part was not needed:

    cout << setprecision( 2 );
    //sets how many spaces after the decimals point

    ex: 5.34

    cout << setprecision( 3 );

    ex: 4.564

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    16
    that is for output and doesn't affect the input or how the data is held in the program.

  12. #12
    Unregistered
    Guest

    thanks for all the trouble

    Thanks for all the trouble, maybe I haven't done my homework correctly but it was hard to find information about this in books or on the internet. I hadn't thouht about the possibility to write a simple program which gave the answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM

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