自定义验证来比较两个文本框的浮点值

时间:2022-11-29 20:37:15

I am using a custom validator to compare value in two text box. This is comparing the values fine. But it says "025" and "25" are different.. can this do a float comparision.

我正在使用自定义验证器来比较两个文本框中的值。这是比较好的价值。但它说“025”和“25”是不同的......这可以做浮动比较。

the custom validator i am using is

我正在使用的自定义验证器是

<asp:CompareValidator id="compval" runat="server" ControlToValidate="txtBox1"
                    ErrorMessage="There values are not equal."
                    Enabled="False" ControlToCompare="txtBox2">*</asp:CompareValidator></TD>

Please let me know if this is possible.

如果可能,请告诉我。

4 个解决方案

#1


1  

Use System.Double.Parse(value) to convert both to a floating point number, and compare those numbers.

使用System.Double.Parse(value)将两者都转换为浮点数,并比较这些数字。

You can also use TryParse if you don't want to handle exceptions if the value is not a valid floating point number.

如果您不希望在值不是有效浮点数时处理异常,也可以使用TryParse。

See also:

#2


1  

The only thing I can think of without seeing your validation code is that 025 is being interpreted as an octal number (in C, putting a zero before an integer means it's in base 8). Then 025 would be 21 in base-10 and your two numbers wouldn't be the same.

在没有看到验证码的情况下,我唯一能想到的是025被解释为八进制数(在C中,在整数之前将零置于基数8中)。然后025将在基数10中为21,而你的两个数字将不相同。

I'm not sure how you'd come up with that, though. I tested out a few of the Parse() functions and they all convert the string "025" to base-10 25.

不过,我不确定你是怎么想出来的。我测试了一些Parse()函数,它们都将字符串“025”转换为base-10 25。

#3


1  

I guess the following is what you need (the question could be phrased a little clearer)

我想以下是你需要的(问题可以说得更清楚)

<asp:CompareValidator ID="cv1" runat="server" ControlToCompare="txt1" ControlToValidate="txt2" Operator="Equal" Type="Integer" ErrorMessage="integers in txt1 and txt2 are not equal" />

#4


0  

use a compare validator with a type of int?

使用类型为int的比较验证器?

#1


1  

Use System.Double.Parse(value) to convert both to a floating point number, and compare those numbers.

使用System.Double.Parse(value)将两者都转换为浮点数,并比较这些数字。

You can also use TryParse if you don't want to handle exceptions if the value is not a valid floating point number.

如果您不希望在值不是有效浮点数时处理异常,也可以使用TryParse。

See also:

#2


1  

The only thing I can think of without seeing your validation code is that 025 is being interpreted as an octal number (in C, putting a zero before an integer means it's in base 8). Then 025 would be 21 in base-10 and your two numbers wouldn't be the same.

在没有看到验证码的情况下,我唯一能想到的是025被解释为八进制数(在C中,在整数之前将零置于基数8中)。然后025将在基数10中为21,而你的两个数字将不相同。

I'm not sure how you'd come up with that, though. I tested out a few of the Parse() functions and they all convert the string "025" to base-10 25.

不过,我不确定你是怎么想出来的。我测试了一些Parse()函数,它们都将字符串“025”转换为base-10 25。

#3


1  

I guess the following is what you need (the question could be phrased a little clearer)

我想以下是你需要的(问题可以说得更清楚)

<asp:CompareValidator ID="cv1" runat="server" ControlToCompare="txt1" ControlToValidate="txt2" Operator="Equal" Type="Integer" ErrorMessage="integers in txt1 and txt2 are not equal" />

#4


0  

use a compare validator with a type of int?

使用类型为int的比较验证器?