CASTING / CONVERTING NUMBERS
Here are a few things that will be useful for your first lab/project.
Depending on your approach, you will have to separate the fractional part of a double value from the whole number part.
Here are two operators that accomplish this task:
- EXTRACT THE WHOLE NUMBER PART: The cast operator. The cast operator is a pair of parentheses with the name of a data type inside.
When placed to the immediate left of an operand, this operator replaces itself and the operand with an immediate value that is of the data type in the parentheses.
Remember that this operator does not modify the operand, it just produces a modified copy of the value -in place- that is an int or whatever type.
Let's look at an example:


Note that the (int) cast operation produces truncation of a double value, not rounding.
- EXTRACT THE FRACTIONAL PART: The modulo operator: We have see the modulo operator (the % sign) used on ints. It also works on doubles. Let's look at an example:


Note that the % (modulo) operator when used with a divisor of 1, isolates the fractional part.