Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I "attempt" to calculate a expression that involves division I'm not getting the correct answer. Example: double number = 5/9; Console.WriteLine(number.ToString());, the result is 0! What's going on here? Why am I not getting .55555...?
Posted
Updated 12-Aug-10 2:44am
v2

what you would need to do is:

double number1 = 5.0/9.0;
double number2 = (double)5/(double)9;
double number3 = 5D/9D;
 
Share this answer
 
v4
Comments
Peter_in_2780 12-Aug-10 0:50am    
number2 will be zero, for the same reason as OP's original problem.
JF2015 12-Aug-10 0:55am    
Updated the code since Peter_in_2780 noticed an error
Roger Wright 12-Aug-10 1:29am    
Reason for my vote of 5
An obscure little item that catches most the first time, properly explained.
Toli Cuturicu 12-Aug-10 8:46am    
5.0 / 9
(double)5 / 9
5D / 9
is enough (no need for the second operand to be double)
thanks. that worked but I don't understand why I had to type cast to double considering that I initially declared "number" a double? would you please clarify this with me?
 
Share this answer
 
v2
Comments
JF2015 12-Aug-10 0:19am    
As I understand it this has been done by design in oo languages.
Here is a link from MSDN that handles the division operation:
http://msdn.microsoft.com/de-de/library/3b1ff23f.aspx
Toli Cuturicu 12-Aug-10 8:44am    
Reason for my vote of 1
fake answer
thank you! I'm about to check out the MSDN link you sent me. Again thank you!
 
Share this answer
 
Comments
Toli Cuturicu 12-Aug-10 8:45am    
Reason for my vote of 1
fake answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900