如何计算范围验证器百分比? [英] How to Calculate Range Validator Percentage?

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

问题描述

我有一个包含10个文本框和5个范围验证的Web表单。前5个文本框具有从数据库填充的数据。其他5个文本框用于输入数据。范围验证器有一个公式,可以确定该值是低于还是高于20%。如果第一个填充的文本框的数据为651684且用户输入651684,则范围验证程序不会因为以下公式而触发:

I have a web form that has 10 textboxes and 5 range validations. The first 5 textboxes have data that is populated from the database. The other 5 textboxes are the for input data. The range validator has a formula that will find out if the value is lower or higher than 20%. If the first populated textbox has a data of 651684 and the user enters 651684 the range validator does not fire because of this formula:

int iMinVal = (itxtVal - itxtVal * 20 / 100);





这意味着textbox1 - textbox2 * 20/100 = 0.所以,0是我这个范围的最小值。



当我尝试设置最大值时,我有这个公式:

.

Which means textbox1 - textbox2 * 20/100 = 0. So, 0 is my minimum value for this range.

When trying to set the maximum value I have this formula:

int iMaxVal = (itxtVal + itxtVal * 20 / 100);





这意味着textbox1 + textbox2 * 20/100 = 260673.6。因此,260273.6比651684高出20%。我正在努力确保我的计算公式正确,以使RangeValidation正确启动。



这是整个我正在使用的公式。



.

Which means textbox1 + textbox2 * 20/100 = 260673.6. So, 260273.6 is 20% higher than 651684. I am trying to make sure I have my calculation formula right to get the RangeValidation to fire correctly.

Here is the whole formula that I am using.

int itxtVal = Convert.ToInt32(TextBox1.Text);
        int iMinVal = (itxtVal - itxtVal * 20 / 100);
        int iMaxVal = (itxtVal + itxtVal * 20 / 100);
        RangeValidator1.MinimumValue = iMinVal.ToString();
        RangeValidator1.MaximumValue = iMaxVal.ToString();





我的计算公式是否正确,以确定textbox2中的值是否正确是否比填充的textbox1中的值高20%或更低?



Is my calculation formula correct to finding out if the value in textbox2 is 20% higher or lower than the value in the populated textbox1?

推荐答案

int itxtVal = Convert.ToInt32(TextBox1.Text);
int iMinVal = (itxtVal - itxtVal * 20 / 100);
int iMaxVal = (itxtVal + itxtVal * 20 / 100);



因此,如果itxtVal = 100,

则iMinVal = 100 - 100 * 20/100 = 100 - 20 = 80,

和iMaxVal = 100 + 100 * 20/100 = 120



所以是的,你的公式是正确的。



UPDATE(来自评论):您的公式可以大大简化,使其更易于阅读和理解。

另外,使用Integer数据类型会导致数字被舍入。如果您不想这样,请使用Double代替。不要忘记将RangeValidator.Type属性设置为Double。




So, if itxtVal = 100,
then iMinVal = 100 - 100 * 20 /100 = 100 - 20 = 80,
and iMaxVal = 100 + 100 * 20 / 100 = 120

So yes, your formula is correct.

UPDATE (from comments): Your formula can be greatly simplified making it easier to read and understand.
Also, using the Integer datatype causes your numbers to be rounded. If you don't want that, use Double instead. Don't forget to set the RangeValidator.Type property to Double as well.

double txtVal = Convert.ToDouble(TextBox1.Text);
double minVal = (txtVal * 0.8);
double maxVal = (txtVal * 1.2);
RangeValidator1.MinimumValue = minVal.ToString();
RangeValidator1.MaximumValue = maxVal.ToString();


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

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