We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In short, this is due to order of operations (PEMDAS). The operation inside the parentheses is evaluated first, and then it is multiplied with dMealCost.
nTipPercent is an int and 100 is an int, and enclosing them in parentheses means that their result will be calculated first. The result of int / int = int; because a % is a fraction < 1, the result evaluates to 0 in integer division (so you're basically multiplying dMealCost * 0). If you change 100 to 100.0, it will evaluate as a double.
Day 2: Operators
You are viewing a single comment's thread. Return to all comments →
In short, this is due to order of operations (PEMDAS). The operation inside the parentheses is evaluated first, and then it is multiplied with
dMealCost
.nTipPercent
is an int and100
is an int, and enclosing them in parentheses means that their result will be calculated first. The result of int / int = int; because a % is a fraction < 1, the result evaluates to 0 in integer division (so you're basically multiplyingdMealCost * 0
). If you change100
to100.0
, it will evaluate as a double.