c# - Why textBox in WinForms cant display double? -
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace p2_7_24_2016_ed_app { public partial class form1 : form { int win; int loss; int wincounter; int losscounter; public form1() { initializecomponent(); } private void buttonwin_click(object sender, eventargs e) { this.textboxwin.text = ""; ++win; ++wincounter; this.textboxwin.text = win.tostring(); } private void buttonloss_click(object sender, eventargs e) { this.textboxloss.text = ""; ++loss; ++losscounter; this.textboxloss.text = loss.tostring(); } private void buttonrate_click(object sender, eventargs e) { textbox1.text = ""; double result = wincounter / (wincounter + losscounter); textbox1.text = result.tostring(); } } }
this code in windows form application, short i'm not going explain everything. problem in end textbox1.text = result.tostring();
showing "0", should numbers. when win wincounter 1, , losscounter 1 should insert 1/(1+1) = 0.5 says "0". tried knew please give me tip.
this integer arithmetic:
(wincounter/(wincounter+losscounter))
the result not going expect.
cast either wincounter
or (wincounter+losscounter)
floating point type (float, double or decimal depending on speed/accuracy need) before doing division:
(wincounter/(decimal)(wincounter+losscounter))
or
((double)wincounter/(wincounter+losscounter))
and you'll result expect.
Comments
Post a Comment