type casting

• 10-17-2001
Unregistered
type casting

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..
• 10-17-2001
Dang
It will truncate the value to an integer value, negative or positive.
• 10-17-2001
Unregistered
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?
• 10-17-2001
Dang
Do you mean 2.6 ? Because double cannot have 2,6.
• 10-17-2001
Unregistered
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
• 10-17-2001
Unregistered
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...
• 10-17-2001
esilja
it should have a value of 2
• 10-17-2001
Unregistered
and -1.5?
and -1.5?
• 10-17-2001
esilja
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-17-2001
esilja
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
• 10-17-2001
esilja
that is for output and doesn't affect the input or how the data is held in the program.
• 10-17-2001
Unregistered
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.