views:

250

answers:

7

double variable = Convert.ToDouble(5/100);

Will return 0.0 but i expected 0.05

What can / must i change to get 0.05

because the 5 in this example is a variable

+12  A: 

5/100 is done in integer arithmetic, which yields 0 before conversion. Try

double variable = 5.0/100;

If 5 is in a variable x (of integer type), then use:

variable = (double)x/100; 

or

variable = ((double)x)/100;

to make the intent clear (thanks John!)

or

variable = x/100.0;
Luther Blissett
-1 Useless use of `Convert`.
Ben Voigt
I undid my downvote because you added a correct example, but you still have bad examples using `Convert.ToDouble` on an expression that's *already* a double. Why?
Ben Voigt
@Ben: Thanks, fixed (I was in a hurry, sorry)
Luther Blissett
@Luther: I suggest changing from "(double)x/100" to "((double)x)/100" to make the intent clearer. Doesn't change the functionality, though.
John
I was also wondering why one would use Convert rather than casting in the first place. Exception handling?
James
I think question asks why it is treated this way.
this. __curious_geek
A: 

Marco,

try this instead:

double variable = Convert.ToDouble((double)5/100);

jim

[late edit] - as was pointed out (by silent voters :)) the use of the Convert is redunant. It should obviously be done in a fashion similar to the other entries. I acknowledge that but leave it in above as an example of how to get quickly downvoted when answering a question in a hurry... let caution be your master!!

Also, as we don't know whether the variable will come from a string or a number, he should also consider tryparse etc on the number 1st before doing the arithmetic.

jim
Useless use of `Convert`, and non-obvious (does casting take precedence over division, or is the division done first?)
Ben Voigt
Ben - thanks for the downvote :-). i'm addressing his question with an answer that matches HIS intended requirement. no more, no less..
jim
all the 1111's - legs eleven :0
jim
+2  A: 

Because 5/100 in integer division is 0. You need to ensure you are doing division on doubles.

James
+1  A: 

double variable = 5D / 100D;

KristoferA - Huagati.com
+2  A: 

5/100 is integer arithmetic. In order to have double precision one or more of the values need to be doubles.

double result = 5.0/100.0;  
double result = 5.0/100;  
double result = 5/100.0;  
double result = (double)5/100;  
double result = 5/(double)100;

or

double numerator = 5;  
double denominator = 100;  
double result = numerator / denominator;
Jerod Houghtelling
+4  A: 

Unlike real-world, computers treat mathematical operations a bit differently though there's no significant difference once we understand why it behaves so.

1.) Why it behaves like this ?

Note that, integers are whole numbers and the variables of type integer can only store whole numbers and can not store or recognize decimal numbers. when you say 5/100, both 5 and 100 are integer literals for the computers and it is called integer division. The result should be 0.05 but since this is an integer division the result would also be integer and as I said integers cannot store decimal point values, the trailing part after "." (decimal point) is ignored completely and hence the result is 0.

Adding more to this, though you're converting the result to double, it does not make any difference because before it is actually converted to a double the result is already 0 and it happens to convert integer 0 to double which ultimately results into 0.0.

2.) How to get your desired output ?

Other answers explain the solution very well, so I kindly request you to refer to those answers rather re-inventing the wheel for you.

Hope this helps.

this. __curious_geek
A: 

so here would be my revised 'considered' answer...

as we don't know what 'type' of variable is arriving as the numerator, we'd have to use the Double.TryParse on that. In the lab, we could cook something like this up:

var numerator = "5"; // let's make it string to prove the point
double parsedNumerator;
int denominator = 100; // this could well be a constant

double result;
if(Double.TryParse(numerator, out parsedNumerator))
{
    // notice no casting or convert fluff
    result = parsedNumerator/denominator;
    // do something with the result
}
else
{
    // warn that the numerator is doo-lallie
}

now hiding under the desk - just in case i've overlooked something else obvious!! :)

jim

jim