Sha*_*oor 3 c# numbers decimal limit digit
我使用以下代码只从用户获取数字和一个小数点,这对我来说在 KeyPress 事件上工作正常:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
现在我想限制小数点/点后的数字/数字,即 35.25468,这意味着点/小数点后只需要 6 个数字/数字。
告诉我 !
private void price_tb_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 &&
textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
{
e.Handled = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码将帮助您。它只需要一位小数和一位小数后两位,您可以相应地更改它。
| 归档时间: |
|
| 查看次数: |
14186 次 |
| 最近记录: |