Raghav55
Raghav55

Reputation: 3135

What regular expression will match decimal values restricted to 6 decimal positions?

I have to write a regular expression that will match the following patterns, it should match decimal values, which can be in the formats shown below.

  1. +100.00
  2. -100.00
  3. .6777
  4. 0.45555
  5. the normal entry of the decimal values like 100 100.25 100.... and also the decimal points should be restricted 6 decimal positions.

This is the regular expression I have written so far:

return Regex.IsMatch(value, "^((\\+|-)(\\d*))+((\\.|,)\\d{0,5})?$");

Currently if the value is like +100 or -100 the reqular expression matches. If I enter a value like 100, it is not accepted and if I start with the a decimal point like .899 then IsMatch returns false.

Upvotes: 1

Views: 358

Answers (3)

Roy Dictus
Roy Dictus

Reputation: 33139

The correct expression is

^((\+|-)?(\d*))+((\.|,)\d{0,6})?$

You can test it at http://www.fileformat.info/tool/regex.htm.

Upvotes: 2

hsz
hsz

Reputation: 152236

You can try with:

^[+-]?\d*([.,]\d{1,6})?$

Upvotes: 2

khachik
khachik

Reputation: 28693

(+|-)?\d*[.,]?\d{0,6}$ and 7 gnomes to be posted.

Upvotes: 0

Related Questions