Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

478
Views
Regex decimal greater than 0 less than 1 with at most 3 decimal places

I want to make a decimal number that is greater than 0 and less than 1, with at most 3 decimal places. So 0, 0.0004, 0.00, 00.004, are NOT valid. But 0.004 is valid.

I thought this shall be simple but couldn't get it working. This is what I came up with: /^0(?:\.)([1-9]{1,3}?$)/g which makes 0.004 invalid but 0.004 is valid.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You may use this regex with a negative lookahead assertion to disallow all zeroes after dot:

^0\.(?!0+$)\d{1,3}$

RegEx Demo

RegEx Details:

  • ^: Start
  • 0\.: Match 0.
  • (?!0+$): Make sure we don't have all zeroes ahead
  • \d{1,3}: Match 1 to 3 digits
  • $: End

Above regex uses anchors assuming only one such number per line. If there are multiple decimal numbers per line then use word boundary instead of anchors:

\b0\.(?!0+$)\d{1,3}\b

If trailing 0 is not be allowed after dot then use:

^0\.\d{0,2}[1-9]$
\b0\.\d{0,2}[1-9]\b

RegEx Demo 2

about 4 years ago · Juan Pablo Isaza Report

0

While regex tend to be short, they're also cryptic. If you're open to alternatives:

const validate = n => {
  n = Number.parseFloat(n) * 1000;
  return Number.isInteger(n) && n > 0 && n < 1000;
};
about 4 years ago · Juan Pablo Isaza Report

0

This regular expression should do the trick

^0\.\d{1,3}$

You can test it here

https://regex101.com/r/IwQsaD/1

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!