Nik
Nik

Reputation: 121

preg_match for float(with check exist fractional part)

preg_match("#^\d+\.?\d*$#",$number);
// 1    - return 1
// 1.2  - return 1
// 2.25 - return 1
// 1.   - return 1 - but this not full number(there is no fractional part) 
//                   and should return 0

I have problem with check number 1. becouse i get 1 instead of 0. For solve problem i tryed use preg_match("#^\d+\.?(?=\d)\d*#",$number); but it not work, becouse in latter case number 1 return 0.

Tell me please why preg_match("#^\d+\.?(?=\d)\d*#",$number); return 0 for number 1 and how fix it ?

Upvotes: 1

Views: 76

Answers (1)

max
max

Reputation: 2817

Try this one :

preg_match("#^\d+(\.\d+)?$#",$number);

Upvotes: 2

Related Questions