Click here to Skip to main content
15,884,625 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Why am I getting various formats of 0.0 , when I divide
C#
int wins = 3;
int games = 12;
double winPercentage = 0;
winPercentage = wins/games;
TextBlock.Text= winpercentage.ToString("assorted formating experiments");

I am trying to display team win percentages.
All I seem to be able to get is 0;
Posted
Updated 30-May-11 7:35am
v3
Comments
yesotaso 30-May-11 13:35pm    
Added pre tag

This has nothing to do with double formatting. Your problem is Integer division. "3/12 = 0.25" which is "0" as integer.
If you tried double wins = 3; or double games = 12; or (double)wins/games or wins/(double)games you'd get it as "0.25".
The division operator would treat both operands as floating point number if you'd supply at least 1 of them as such. But since both operands are integer result does not hold floating point.
 
Share this answer
 
v2
Comments
o1010wanabe 30-May-11 13:55pm    
Thank you so much for the rapid solution.
yesotaso 30-May-11 13:56pm    
No problem.
Sergey Alexandrovich Kryukov 30-May-11 15:44pm    
Correct, a 5.
--SA
Do it like this:

winPercentage = (double)wins/games;

You need to cast it to double.
 
Share this answer
 
Comments
o1010wanabe 30-May-11 13:55pm    
Thanks a million.
Tarun.K.S 30-May-11 13:56pm    
Glad to help! :)
Sergey Alexandrovich Kryukov 30-May-11 15:44pm    
Typical mistake. A 5.
--SA
Tarun.K.S 31-May-11 2:36am    
Thanks SA. :)

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