如何计算文本框中的百分比? [英] how to calculate percentage in textbox?

查看:157
本文介绍了如何计算文本框中的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,

我想在我的项目中计算百分比。我写了这个代码它正在工作但输出值0只显示。



Hi Friend,
I want calculate percentage in my project.I wrote this code it is working but in output value 0 only displayed.

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



temp在全球宣布。



请解决我的问题。


temp declared in global.

please solve my problem.

推荐答案

使用以下内容(假设文本框中包含非零数字):

Use the following (assuming the textboxes have non zero numbers in them) :
temp = int.Parse(txtpresent.Text)*100 / int.Parse(txtworkingdays.Text);


您将两个值都转换为int然后除以



而不是你将数字换成浮点数,你会得到结果。



试试这个

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
private void txtpresent_Leave(object sender, EventArgs e)
        {
            temp = (float.Parse(txtpresent.Text) / float.Parse(txtworkingdays.Text)*100);
            txtpercentage.Text = temp.ToString();
        }


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();

        }


这篇关于如何计算文本框中的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆