数字必须是数字,最多9位数 [英] number must be numeric up to 9 digits

查看:104
本文介绍了数字必须是数字,最多9位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



你有什么想法,我怎么写浮点数numbet但数字不会超过9。



我使用下面的代码,但我不能绑定9位数。我应该添加一些代码以及如何添加代码?



hi all,

do you have any idea, how can i write floating point numbet but digit wil not be more than 9.

I used below code but i can''t bind in 9 digit. what should i add some code and how?

Regex regex = new Regex(@"((\b[0-9]+)?\.)?[0-9]+\b");





提前致谢

Shafik



Thanks in advance
Shafik

推荐答案

感谢您的澄清。它最终看起来像Regex根本没用。



在输入数据之前,执行过滤,过滤除十进制数字以外的所有内容,''。''和退格(代码点8)。不幸的是,蒙面编辑不会有太多帮助。例如,如果这是 System.Windows.Controls.TextBox ,则处理事件 TextInput 或重载 OnTextInput 方法。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.cont rols.textbox%28v = vs.95%29.aspx [ ^ ]。



设置 System.Windows.Input.TextCompositionEventArgs.Handled 取消错误输入字符:

http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs%28v=vs.95%29.aspx [ ^ ]。



输入数据后,在即将使用或更早的数据时,例如,在失去焦点时,执行输入验证。不要在乎额外的数字。只需执行 double.TryParse 即可。如果没有失败,您可以另外验证该值是否在所需范围内或满足任何其他数学标准。



其他一切都不可靠。 TryParse 方法真正知道字符串是否有效解析为一种或另一种数字类型。变种太多,解析算法足够复杂;你不想模仿它。



-SA
Thank you for clarification. It finally looks like Regex is not helpful at all.

Before entering data, perform filtering, to filter out everything except decimal digits, ''.'' and backspace (code point 8). Unfortunately, masked edit won''t help much. For example, if this is System.Windows.Controls.TextBox, either handle the event TextInput or overload OnTextInput method. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox%28v=vs.95%29.aspx[^].

Set System.Windows.Input.TextCompositionEventArgs.Handled to cancel input of a wrong character:
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs%28v=vs.95%29.aspx[^].

After entering data, at the moment when the data is about to be used, or earlier, for example, on loosing focus, perform the validation of input. Don''t care about extra digits. Simply perform double.TryParse. If it is not failed, you can additionally validate that the value is in required range or meet any other math criteria.

Everything else would be unreliable. The TryParse methods truly "know" if the string is valid for parsing to one or another numeric type. There are too many variants, and algorithm of parsing is complex enough; you don''t want to emulate it.

—SA


试试这个:< br $>


^ [0-9] {1,9}(\。[0-9] +)?
Try this:

^[0-9]{1,9}(\.[0-9]+)?


< br $>


JAFC


JAFC


这篇关于数字必须是数字,最多9位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆