Check the input string with a valid decimal number

Can someone provide an algorithm that checks if the input string is a decimal number in the correct form and form?

Rules of the correct form and form:

  • No more than two decimal places.
  • For all practical purposes, the largest number is 99,999,999.99
  • The integral part may use a space or a comma or a period to separate groups.
  • The decimal part can use a comma or a period for the delimiter.

Examples of the correct shape and shape:

1,234,567.89 // English style
1.234.567.89 // French style
1 234 567.89 // German style
1234567.89 // English mathematical style
1234567.89 // European mathematical style
12.00
12.0
12

, Decimal.Parse Decimal.TryParse. , "1,2,3,4", , .

+3
3

, , . - , , , .

, :

English: ^[-+]?\d{1,3}(,\d{3})*(\.\d+)?$
French: ^[-+]?\d{1,3}(\.\d{3})*(,\d+)?$
German: ^[-+]?\d{1,3}(\s\d{3})*(,\d+)?$
English mathematical: ^[-+]?\d+(\.\d+)?$
European mathematical: ^[-+]?\d+(,\d+)?$

:

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

, . , , , .

Rubular : http://rubular.com/r/Dipvyrf6C8 ( , ).

: [-+]?, .

+5
0

, , :

var s = "1 334 567,80";
var decimalPart = s.Split(',', '.').Last();
var integerPart = s.Substring(0, s.Length - decimalPart.Length - 1);
integerPart = integerPart.Replace(",", string.Empty).Replace(".", string.Empty)
                         .Replace(" ", string.Empty);

var decimalValue = decimal.Parse(integerPart + "." + decimalPart,
                                 CultureInfo.InvariantCulture);

, , .

0

All Articles