Click here to Skip to main content
15,886,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

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


Thanks in advance
Shafik
Posted
Updated 21-Mar-13 6:55am
v2
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 11:12am    
.NET? C#? VB.NET? This is important, because you really need non-Regex solution. Regex won't comprehensively solve it, but non-Regex solution is fully comprehensive and is much easier. Also, if this is the user input in UI, you can additionally filter input first, and later also validate.

As your case looks a simple special case, you can still use Regex, but then, how many digits after '.' do you want to allow?
—SA
[no name] 21-Mar-13 11:23am    
Thanks for ur reply. I am working in C# WPF Application. Total will be 9 digit. example: 1.12345678,12345678.9 etc. I don't care how many after point. But never more than 9 digit.
Please help what i add with that. Thanks
Sergey Alexandrovich Kryukov 21-Mar-13 11:30am    
I would say, bad criteria. Do you mean that 1.1234 is good, but 1123.4 as well, all combinations? Then Regext is no good. But why? Could you criteria be "valid floating-point" in some given range? For the application, it could be the best.
Again, is it UI user input?
—SA
[no name] 21-Mar-13 11:36am    
Thanks SA, Yes this is user input. thats why i am telling after point i don't know how many they input? but please tell me what i will add in my RgEX. User can not write more than 9 digit. thanks
Sergey Alexandrovich Kryukov 21-Mar-13 12:23pm    
OK, this is where you should have started. I'll answer...
—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
 
Share this answer
 
Comments
Maciej Los 21-Mar-13 13:00pm    
Perfect, as always.
+5!
Sergey Alexandrovich Kryukov 21-Mar-13 13:08pm    
Thank you very much, Maciej,
—SA
Try this:

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

JAFC
 
Share this answer
 
Comments
[no name] 21-Mar-13 11:28am    
Thanks JAFC i used that Regex regex=new Regex(@"^[0-9]{1,9}(\.[0-9]+)?$"); but it takes more than 9 digit.
José Amílcar Casimiro 21-Mar-13 11:42am    
The integer part will only accept 9 digit. The requirement is 9 digits in total including the point?
[no name] 21-Mar-13 11:51am    
Exaclty, Including point total will be Nine digit. Thanks for your reply
José Amílcar Casimiro 21-Mar-13 11:56am    
.ToString().Length > 9 ??? does not do the job ?
[no name] 21-Mar-13 12:06pm    
thanks Let see

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900