Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx needed to match number to exactly two decimal places

Tags:

I need some regex that will match only numbers that are decimal to two places. For example:

  • 123 = No match
  • 12.123 = No match
  • 12.34 = Match
like image 708
geoffs3310 Avatar asked Jan 14 '11 12:01

geoffs3310


People also ask

What is the regex for decimal number?

\d* - 0 or more digits (the decimal part);

How do you move a number to 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

Which regex matches one or more digits?

Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.


1 Answers

^[0-9]*\.[0-9]{2}$ or ^[0-9]*\.[0-9][0-9]$
like image 196
Paul Avatar answered Sep 29 '22 11:09

Paul