C# RegEx for decimal number

蹲街弑〆低调 提交于 2019-12-25 01:16:31

问题


I'm trying to make a Regular expression for a textbox which should only allow either only digits or decimals. For example, I should be able to enter both 10 and 10,350.

The decimal seperator I'll use is "," and the decimal length don't need a limit.

Anyone knows how I can make such a RegEx?


回答1:


I'd go for decimal.TryParse() too, but if you really need a RegEx then something like this should work: (\d+(,\d*)?)




回答2:


Just use decimal.TryParse or double.TryParse.




回答3:


if you will use it in many place in your project you can make new TextBoxcontrol it accept just number and make ',' when even the '.' clicked, and you can make test on it when it lostfocus()




回答4:


Here's your regex:

^[\d]+,?[\d]*$

check if it matches what's in your textbox.



来源:https://stackoverflow.com/questions/6087620/c-sharp-regex-for-decimal-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!