Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

i have a text box which accepts numeric and decimal values. The max length of text box is 4.

So how can i check whether a text box contains a decimal point followed by a single digit at end of text box.

like ex:

if i have a value as 12.5 in the text box,then how can i check that a text box is containing a value ".5" at the end of textbox in C#


please provide me a solution.
Posted

string a = "12.5";


            bool HasDecimal = a.Contains('.');


            if (HasDecimal == true)
            {
               int i = a.IndexOf('.');
               int j = a.Length;
               int k = j - i;
               a = a.Substring(i, k);
// now a contains only decimal value

            }
        else {
//no decimal
}
   
v2
Comments
Member 10593922 14-Mar-14 2:41am
   
thank u for the reply. i will try to implement this... but what about the digit after decimal point how can i check that?
AndrewCharlz 14-Mar-14 3:13am
   
the out put will be ".5"
Try this

if(TestBox1.Text.Contains('.')&&TextBox1.Text.Split('.')[1].Length==1)
{
  //code
}
   
v2
private static readonly Regex OneDecimalCheck = new Regex(@"\.\d$");
// ....
if (OneDecimalCheck.IsMatch(TheTextBox.Text))
{
  // TheTextBox content Text ends with . followed by a digit.
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900