Selective check to compare float value of two text fields

I am using a custom validator to compare the value in two text fields. This compares the penalty values. But he says that "025" and "25" are different. Can floating point comparison do it.

the custom validator I'm 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 possible.

+1


source to share


4 answers


Use System.Double.Parse (value) to convert both numbers to floating point and compare the numbers.

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



See also:

+1


source


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



I'm not sure how you came up with this. I checked several Parse () functions and they all convert the string "025" to base-10 25.

+1


source


I guess what you need (the question could be phrased a little more clearly)

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

      

+1


source


use a comparison validator with type int?

0


source







All Articles