Unable to restrict number of decimal digits when parsing a string to decimal in C#
问题 I'm trying to parse a string to a decimal and the parsing should failed if there are more than 2 digits after the decimal point in the string. e.g: 1.25 is valid but 1.256 is invalid. I tried to use the decimal.TryParse method in C# to solve in the following manner but this does not help... NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalDigits = 2; if (!decimal.TryParse(test, NumberStyles.AllowDecimalPoint, nfi, out s)) { Console.WriteLine("Failed!"); return; } Console