Lesson 9: Division & double
A surprise in C#: when you divide two whole numbers, the result is cut to a whole number — 7 / 2 gives 3, not 3.5! The fraction is simply dropped. To get a fraction you need the double type (a number with a decimal point): 7.0 / 2 gives 3.5. In this lesson we'll learn when to divide with int and whe
Integer division is like splitting whole cakes without cutting: 7 over 2 gives 3 cakes each, and the leftover half is thrown away. double is like allowing cuts — so you get 3.5.
- integer division
- When an int is divided by an int, the result is cut to a whole number (the fraction is dropped). 7 / 2 = 3.
- double
- A type for a number with a decimal point, e.g. 3.5, 0.25, 10.0.
- double division
- If at least one number is a double, the result keeps the fraction. 7.0 / 2 = 3.5.