Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Can anyone help me with the regular expression for any number from 0 - 10 with maximum of 2 decimal

Valid numbers:
0.23
1.02
6.2
7.20
10.00
10

Invalid numbers:
10.01
12.333
0.233

Please help
Posted
Updated 14-Dec-21 22:20pm

This will work:
^(10|\d)(\.\d{1,2})?$

(10|\d) allows for a single digit or 10
(\.\d{1,2})? possible decimal followed by 1 or 2 digits
 
Share this answer
 
^((10)(\.[0-0]{0,2})?$|([0-9])(\.[0-9]{1,2})?$)


the above regex code work for 0.00 to 10.00


^((0)((\.0[1-9]{1})?$|(\.[1-9][0-9]{0,2})?$)?$|(10)(\.[0-0]{0,2})?$|([1-9])(\.[0-9]{1,2})?$)
this is for 0.01 to 10.00
 
Share this answer
 
v2
Comments
Patrice T 15-Dec-21 4:26am    
Already answered 8 years ago. Try more recce'nt questions.
CHill60 15-Dec-21 8:21am    
You may not be aware, we have been asked to allow new solutions to old posts (by Chris and Sean) - especially if they add something new, or in this case, are correct when the other two explicit solutions (1 and 3) are not
CHill60 15-Dec-21 8:19am    
I have countered the downvote on your solution because, unlike solutions 1 and 3, your first regex handles all of the examples given and rejects the specific invalid numbers listed.
Your second regex is irrelevant and does not match the question requirements
Try:
^((1\d)|(\d))(\.(\d\d)|\d){0,1}$
 
Share this answer
 
Comments
Brian A Stephens 20-Sep-13 10:10am    
Your regex allows numbers 11-19 also, which shouldn't be included.

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