tags:

views:

220

answers:

11

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 ... :(

+3  A: 

You need to convince the compiler to perform floating point division:

double result = (((double)3/8)*100);

Otherwise it performs integer division and 3/8 is zero then.

sharptooth
Use decimal point in the constants and you can avoid that ugly cast :)
snemarch
...and there's the issue of .ToString() as well :)
snemarch
True about ToString() - completely missed it. The ugly cast makes it look more explicit.
sharptooth
+6  A: 

3/8 performs an integer division and the result is 0

double result = ((3.0/8)*100);

should do it.

By the way, if you do ((3.0/8)*100).ToString() you get a String and not a double.

Burkhard
+3  A: 
double result = ((3.0/8.0)*100);

Should do it. You were performing integer division, not floating point division.

+1  A: 

The 3 and the 8 are integers, so 3/8 = 0.

Use:

string result = ((3d/8d)*100d).ToString();
teedyay
This won't compile ;-) Make result a string or remove .ToString()
0xA3
Oopsy - right you are. Fixed now. Thanks.
teedyay
A: 

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.

David M
+1  A: 

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)

soulmerge
+1  A: 

3 & 8 are integers, so the result of 3/8 is also an integer unless you cast it differently.

So, 3/8 = 0.

+1  A: 

The unicorns took away the 3 and replaced it with a 0.

On the off chance that this is a real question... 3/8 is rounded down to 0. 0* 100 = 0.

TofuBeer
+3  A: 

Your expression involves only integers, and 3/8 is 0.

If you want a floating-point expression, at least one element of the expression must be floating point.

The simplest solution is the following:

double result = (100.0 * 3/8)

Note that I put the hundred factor first because it helps having a better precision to do the multiplications before the divisions.

Also, the toString() is strange??

cadrian
Multiplying first is mostly irrelevant when you use floating point, it is useful when you want to stay in integers.
starblue
A: 

3/8 = 0

3.0/8.0 = 0.375

Adrian
A: 

Because integer math was used, not floating point.

You wrote ((3/8)*100) but none of the constants in that expression have a non-integral type. Therefore the compiler (correctly) interpreted that as integer arithmetic. Since 3/8 is less than 1, the expression evaluates to 0.

A simple fix would be to write ((3./8.)*100.) instead. Actually, making either of the 3 or 8 be a floating point value would be sufficient due to the rules for mixed type expressions.

RBerteig