Decimals missing
-
Hello. I want to do something like this: float number = 3 / 2; The problem is that the decimals don't show up, all that is displayed is "1" instead of 1.5. I have tried using float/double/decimal but still the decimals disappear. What's wrong? Am I missing something here?
-
Hello. I want to do something like this: float number = 3 / 2; The problem is that the decimals don't show up, all that is displayed is "1" instead of 1.5. I have tried using float/double/decimal but still the decimals disappear. What's wrong? Am I missing something here?
I think you are. In this expression, the numbers 3 and 2 are integers. If you integer divide 3 by 2 you get 1. This 1 is converted to a float implicitly because no data is lost, but it's not the value you are looking for. Try:
float number = 3.0 / 2.0;
Regards, Rob Philpott.