Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friend,
I want calculate percentage in my project.I wrote this code it is working but in output value 0 only displayed.

C#
private void txtpresent_Leave(object sender, EventArgs e)
        {
            temp = (int.Parse(txtpresent.Text) / int.Parse(txtworkingdays.Text)*100);
            txtpercentage.Text = temp.ToString();
        }

temp declared in global.

please solve my problem.
Posted
Updated 29-Sep-11 19:21pm
v2

Use the following (assuming the textboxes have non zero numbers in them) :
C#
temp = int.Parse(txtpresent.Text)*100 / int.Parse(txtworkingdays.Text);
 
Share this answer
 
You are converting both values to int and then dividing
so since int type does not return decimal value it is giving zero as output.

instead you convert the numbers to float, you will get the results.

try this
C#
private void txtpresent_Leave(object sender, EventArgs e)
        {
            temp = (float.Parse(txtpresent.Text) / float.Parse(txtworkingdays.Text)*100);
            txtpercentage.Text = temp.ToString();
        }
 
Share this answer
 
Comments
Mehdi Gholam 30-Sep-11 1:32am    
OP : Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
Hari Krishna Prasad Inakoti 30-Sep-11 1:33am    
Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
i got this error.
P.Salini 30-Sep-11 1:35am    
If you want int value then you need to convert it to int explicitly
I gave it in float because generally while calculating percentages we use float.
C#
private void txtpresent_Leave(object sender, EventArgs e)
        {
        decimal    temp = (Math.Round(Convert.ToDecimal(txtpresent.Text) / Convert.ToDecimal(txtworkingdays.Text)*100),0);
            txtpercentage.Text = temp.ToString();

        }
 
Share this answer
 
add this % button on calculator with only 1 textbox(i/p & o/p).
 
Share this answer
 
ivalue = int.Parse(txtbox1.Text)*100 / int.Parse(txttxtbox2.Text);
 
Share this 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