如何汇总文本框 [英] How do roundup a textbox

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

问题描述

我以为我可以正确地四舍五入,但是在我将它用于获取textBox5值的部门中之前,我似乎设置了textBox4值.综述必须在代码广告的第一部分中,我无法弄清楚.有人可以帮忙吗?

I thought I was rounding up correctly but looks like I set textBox4 value before I use it in the division for getting textBox5 value. The Roundup needs to be in the first part of the code ad I cant figure it out. Can anyone help?

var roundedInput = Math.Round(tbTrailer_Needed, 2, MidpointRounding.AwayFromZero);

if (!String.IsNullOrEmpty(tbSnp_Day.Text) && !string.IsNullOrEmpty(cbSnp_Uld_Trl.Text))
    tbTrailer_Needed.Text  =  (Convert.ToInt32(tbSnp_Day.Text) / Convert.ToInt32(cbSnp_Uld_Trl.Text)).ToString();



我尝试过的事情:

Math.Round,MidpointRounding模式



What I have tried:

Math.Round, MidpointRounding mode

推荐答案

首先,舍入仅适用于浮点值-尝试舍入整数将不起作用.
其次,除以任何数字(非零)将始终得到相同的值:1.最高值为1.
第三,帮个忙,停止对所有内容使用Visual Studio默认名称-您可能还记得"TextBox8"是今天的手机号码,但是当您必须在三周时间内对其进行修改时,您会吗?使用描述性名称-例如"tbMobileNo"-您的代码将变得更易于阅读,更多自我记录,更易于维护-并且出乎意料地更快地编写代码,因为Intellisense可以通过三个按键进入"tbMobile",其中"TextBox8"需要思考大约8次击键...

首先进行转换-而是使用TryParse,因为它允许您响应用户输入错误而不是应用程序崩溃-然后使用转换后的值,最后生成输出:
First off, rounding only works with floating point values - trying to round integers is not going to work.
Second, dividing any number (other than zero) by itself will always give the same value: one. And the Ceiling value of one is one.
Third, do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...

Do your conversions first - and use TryParse instead, as it allows you to respond to user input errors instead of your application crashing - then use the converted values, and finally generate your outputs:
double myValue;
if (string.IsNullOrWhitespace(myTextBox.Text) || !double.TryParse(myTextBox.Text, out myValue))
   {
   // Report problem to user
   return;
   }
double myOtherValue;
...
//Use myValue and myOtherValue, and myYetAnotherValue here and generate your results.
double myResult = ...
//And finally output.
myOutputTextBox.Text = myResult.ToString();

更清楚了正在发生的事情,并且更容易调试出问题所在.

It''s a lot clearer what is going on, and a whole lot easier to debug where a problem is.


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

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