private void textBox1_TextChanged(object sender, EventArgs e)
{
//得到用户输入
string getStr = textBox1.Text;
if (getStr.Length <= 14)//最多允许输入9位数字(加两个','和'.'和小数点后两位总共14)
{
char[] myChar = getStr.ToCharArray();
//清除符号
string clearStr = "";
foreach (char var in myChar)
{
if (var != '.' && var != ',')
{
clearStr += var;
}
}
//转换成Char数组,再颠倒顺序
char[] clearStrDesc = clearStr.ToCharArray();
Array.Reverse(clearStrDesc);
//加符号后的显示(顺序颠倒的)
string markStrDesc = "";
//加符号
for (int i = 0; i < clearStrDesc.Length; i++)
{
if (clearStrDesc.Length == 1 || clearStrDesc.Length == 2)
{
markStrDesc = clearStr;
return;
}
if (i == 2)
{
markStrDesc += "." + clearStrDesc[i];
}
if (i == 5 || i == 8)
{
markStrDesc += "," + clearStrDesc[i];
}
if (i != 2 && i != 5 && i != 8)
{
markStrDesc += clearStrDesc[i];
}
}
//把加符号的字符串转换为Char数组
char[] c3 = markStrDesc.ToCharArray();
//倒回来
Array.Reverse(c3);
//显示
string displayStr = new string(c3);
textBox1.Text = displayStr;
}
else
{
string constNum = getStr.Substring(0, 14);
textBox1.Text = constNum;
}
}
{
//得到用户输入
string getStr = textBox1.Text;
if (getStr.Length <= 14)//最多允许输入9位数字(加两个','和'.'和小数点后两位总共14)
{
char[] myChar = getStr.ToCharArray();
//清除符号
string clearStr = "";
foreach (char var in myChar)
{
if (var != '.' && var != ',')
{
clearStr += var;
}
}
//转换成Char数组,再颠倒顺序
char[] clearStrDesc = clearStr.ToCharArray();
Array.Reverse(clearStrDesc);
//加符号后的显示(顺序颠倒的)
string markStrDesc = "";
//加符号
for (int i = 0; i < clearStrDesc.Length; i++)
{
if (clearStrDesc.Length == 1 || clearStrDesc.Length == 2)
{
markStrDesc = clearStr;
return;
}
if (i == 2)
{
markStrDesc += "." + clearStrDesc[i];
}
if (i == 5 || i == 8)
{
markStrDesc += "," + clearStrDesc[i];
}
if (i != 2 && i != 5 && i != 8)
{
markStrDesc += clearStrDesc[i];
}
}
//把加符号的字符串转换为Char数组
char[] c3 = markStrDesc.ToCharArray();
//倒回来
Array.Reverse(c3);
//显示
string displayStr = new string(c3);
textBox1.Text = displayStr;
}
else
{
string constNum = getStr.Substring(0, 14);
textBox1.Text = constNum;
}
}