I think there's a lot for me to learn about data types. Why this happens
double result = ((3/8)*100).ToString();
it gives zero .. should be 37,5 ... :(
I think there's a lot for me to learn about data types. Why this happens
double result = ((3/8)*100).ToString();
it gives zero .. should be 37,5 ... :(
The 3 and the 8 are integers, so 3/8 = 0.
Use:
You need to convince the compiler to perform floating point division:
Otherwise it performs integer division and 3/8 is zero then.
Should do it. You were performing integer division, not floating point division.
3 and 8 in your division are integer literals, so integer division is performed, and 3 / 8 evaluates to zero. If you replace them with 3.0 and 8.0, or use an appropriate data type suffix (you don't say what language you are in) then the calculation will work.
3 & 8 are integers, so the result of 3/8 is also an integer unless you cast it differently.
So, 3/8 = 0.
The integer division (3/8) yields 0. If you want to work with floating point values, make that clear to your programming language (3.0/8.0 or 3f/8f or 3d/8d or whatever else your language allows)