How can I parse a number equivalent to DateTime.TryParseExact?

I have a requirement when a client needs to check a bunch of Excel tables. One of the verification rules is that this field must be a number that looks like "x.xx" (so that "3.00" is valid, "3.000" is not). It should be as simple as Double.TryParseExact (s, "#. ##", out number), but I think it is not. Is there a way to do this without using regular expressions? The client uses DSL, and I do not want them to learn regular expressions.

+3
source share
1 answer

I suppose you could do this?

public static bool TryParseExact(string text, string format, out double value)
{
    if (double.TryParse(text, out value))
    {
        // basically, make sure that formatting the result according to the
        // specified format ends up providing the same value passed in
        return text == value.ToString(format);
    }

    return false;
}

This is just an idea; To be completely honest, I didn’t really think that all this was happening.

+3

Source: https://habr.com/ru/post/1762356/


All Articles