The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
Home Function Converting from Double to Int | ||
See also: Floating-Point | ||
Converting from Double to Int
The simplest way to convert a floating-point value to an integer is to use a typecast. Typecasting is so called because it allows you to take a value that belongs to one type and "cast" it into another type (in the sense of molding or reforming, not throwing). The syntax for typecasting is like the syntax for a function call. For example: double pi = 3.14159;int x = int (pi); The int function returns an integer, so x gets the value 3. Converting to an integer always rounds down, even if the fraction part is 0.99999999. For every type in C++, there is a corresponding function that typecasts its argument to the appropriate type.
|
||
Home Function Converting from Double to Int |