# Regex for Decimal Numbers

> Decimal number regex matching 42, -7 and 3.14, with an optional minus sign and fractional part. Variations for thousands separators and leading dots.

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

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.

## Token by token

| Token | Meaning |
|---|---|
| `^` | 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 |

## Examples

- ✓ matches: `42`
- ✓ matches: `-7`
- ✓ matches: `3.14`
- ✓ matches: `-0.5`
- ✓ matches: `1000.0`
- ✗ does not match: `.5`
- ✗ does not match: `5.`
- ✗ does not match: `1,000`
- ✗ does not match: `+3`
- ✗ does not match: `1e5`

## 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.

## FAQ

### 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

- [Regex for Numbers Only (Digits)](https://www.devkult.com/regex/digits-only.md)
- [Regex for Alphanumeric Strings](https://www.devkult.com/regex/alphanumeric.md)
- [Regex for Semantic Versions (SemVer)](https://www.devkult.com/regex/semver.md)

Open the interactive page: https://www.devkult.com/regex/decimal-number