Click here to Skip to main content
15,892,927 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

C#
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
}
 
Share this answer
 
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

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

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