tags:

views:

20731

answers:

7

I know this may be the simplest question ever asked on Stack Overflow, but 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

Sorry for the lame question, but for the life of me I haven't been able to find anyone that can help!

The decimal place may be option, and that integers may also be included.

+23  A: 

Valid regex tokens vary by implementation. The most generic form that I know of would be:

[0-9]+(\.[0-9][0-9]?)?

The most compact:

\d+(\.\d{1,2})?

Both assume that you must have both 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):

^\d+(\.\d{1,2})?$

ADDED: Wrapped the fractional portion in ()? to make it optional. Be aware that this excludes forms such as "12." Including that would be more like ^\d+\.?\d{0,2}$.

DocMax
Thanks... The Last regexp was exactly what i was looking for!!!
Mathematically, I think a precision 2 number should always have two decimals even if the last is zero. This is based on my experience with significant figures so it could be wrong, but you don't actually know if 1.7 is 1.70 or any number from 1.70 to 1.74.
paxdiablo
`^\d+(?:\.\d{1,2})?$`
Brad Gilbert
None of these regexes will match .21
Jan Goyvaerts
Thanks! Was exactly what I needed :) +1
alex
+2  A: 

Perhaps one of these is useful for you. By the way, that was the first google hit for "regular expression decimal precision two".

schnaader
A: 

Won't you need to take the e in the e666.76 into account

With (e|0-9)\d*\d.\d{1,2)

spacemonkeys
No, That would be out of the scope of the project! thanks though, it is handy:)
Opps sorry, should have read in more detail !!
spacemonkeys
Haha, Thanks a million though!
+12  A: 
^[0-9]+(\.[0-9]{1,2})?$

And since regular expressions are horrible to read, much less understand, here is the verbose equivalent:

^                   # Start of string.
[0-9]+              # Must have one or more numbers.
(                   # Begin optional group.
    \.              # The decimal point, . must be escaped, 
                    # or it is treated as "any character".
    [0-9]{1,2}      # One or two numbers.
)?                  # End group, signify it's optional with ?
$                   # End of string.

You can replace [0-9] with \d in most regular expression implementations (including PCRE, the most common). I've left it as [0-9] as I think it's easier to read.

Also, here is the simple Python script I used to check it:

import re
deci_num_checker = re.compile("""^[0-9]+(\.[0-9]{1,2})?$""")

valid = ["123.12", "2", "56754", "92929292929292.12", "0.21", "3.1"]
invalid = ["12.1232", "2.23332", "e666.76"]

assert len([deci_num_checker.match(x) != None for x in valid]) == len(valid)
assert [deci_num_checker.match(x) == None for x in invalid].count(False) == 0
dbr
A: 

I've tried this to allow values from 0 to 100 (including integers, 100.0 and 100.00). Seems to be working. (?!^0*$)(?!^0*.0*$)^\d{1,2}(.\d{1,2})|([0-9]{1,2}|[0-9]{1,2}.0|[0-9]{1,2}.00)?(100|100 .0|100.00)?$

A: 

how about also control a posible overflow??? what regular expression can it be

edu
A: 

to include an optional minus sign and to disallow numbers like 015 (which can be mistaken for octal numbers) write:

-?(0|([1-9]\d*))(\.\d+)?

Karol Bienkowski