This is just a way of "rounding" a number to the hundredths digit. First, you multiply by 100 to get the two digits you want to keep on the left side of the decimal, then you cast to int to truncate or discard the decimal portion and finally you divide by a 100.0 to move the two digits you saved to the right of the decimal point again.
So if you compute line 14 it would be 66.6666 and what we want is a number that only has 2 decimal places(to represent the remaining change).
My next question is why is the output for line 16 only have 2 decimal places. My understanding is you are first turning tax*100 into an integer but then you are dividing it by 100.0 which allows the answer to have a decimal, but why only 2 decimal places? I would guess that both lines 14 and 16 would produce the same output but clearly I am wrong. Thanks for your help.
Bjarne Stroustrup is the guy who created C++. I was making a play on words for the phrase "in god's name".
Bansee1 wrote:
My next question is why is the output for line 16 only have 2 decimal places. My understanding is you are first turning tax*100 into an integer but then you are dividing it by 100.0 which allows the answer to have a decimal, but why only 2 decimal places? I would guess that both lines 14 and 16 would produce the same output but clearly I am wrong.
Try doing the math manually:
66.6666
*100 = 6666.66 (moved decimal two places to right)
to integer = 6666
/100.0 = 66.66 (moved decimal place two places to left)
Recall that when multiplying and dividing by powers of ten you may simply count the number of 0s and move the decimal place accordingly ;)
Ok thanks for the explanation LB. My mistake was when I was doing the math for int I did not convert it to an integer w/ no decimal. Now it makes sense. Thanks.