如何检查文本框中是否包含小数点后跟文本框中的单个数字
问题描述:
嗨。
我有一个接受数字和小数值的文本框。文本框的最大长度为4.
那么如何检查文本框是否包含小数点后跟文本框末尾的单个数字。
如前:
如果我在文本框中输入值为12.5,那我怎么能检查文本框在C文本框的末尾是否包含值.5#
请给我一个解决方案。
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.
答
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
}
试试这个
Try this
if(TestBox1.Text.Contains('.')&&TextBox1.Text.Split('.')[1].Length==1)
{
//code
}
private static readonly Regex OneDecimalCheck = new Regex(@"\.\d