What is the regular expression for a decimal with a precision of 2?
Valid examples:
123.12 2 56754 92929292929292.12 0.21 3.1
Invalid examples:
12.1232 2.23332 e666.76
The decimal point may be optional, and integers may also be included.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Valid regex tokens vary by implementation. A generic form is:
More compact:
Both assume that both have at least one digit before and one after the decimal place.
To require that the whole string is a number of this form, wrap the expression in start and end tags such as (in Perl’s form):
To match numbers without a leading digit before the decimal (
.12) and whole numbers having a trailing period (12.) while excluding input of a single period (.), try the following:Added
Wrapped the fractional portion in
()?to make it optional. Be aware that this excludes forms such as12.Including that would be more like^\d+\\.?\d{0,2}$.Added
Use
^\d{1,6}(\.\d{1,2})?$to stop repetition and give a restriction to whole part of the decimal value.