devkult_
tools29converters34
home/regex/decimal-number

Regex for Decimal Numbers

This pattern accepts integers and decimals with an optional leading minus: 42, -7, 3.14, -0.5. Both sides of the decimal point need digits, so .5 and 5. are rejected — the first variation loosens that if your users type them.

/^-?\d+(?:\.\d+)?$/

How it works, token by token

TokenMeaning
^start of string
-?an optional minus sign
\d+the integer part: one or more digits
(?:\.\d+)?optionally, a dot and one or more fractional digits
$end of string

What it matches

✓42
✓-7
✓3.14
✓-0.5
✓1000.0
✗.5
✗5.
✗1,000
✗+3
✗1e5

Try it live

One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.

/

Variations

^[+-]?(?:\d+\.?\d*|\.\d+)$also allow a plus sign, .5 and 5.
^-?\d{1,3}(?:,\d{3})*(?:\.\d+)?$with thousands separators: 1,234,567.89

Language notes

  • Many locales write 3,14 with a comma as the decimal separator and use dots or spaces for thousands. Normalise locale-formatted input before validating, or validate per locale.

Frequently asked questions

Does this accept scientific notation?

No. For values like 1e5 or 6.02E23, add an exponent: ^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$.

How do I limit decimal places, for money?

Bound the fractional part: ^-?\d+(?:\.\d{1,2})?$ allows at most two decimals. Then store money as integer cents, not floating point.

Related patterns

Building something custom? The regex tester gives you live match highlighting for any pattern.