I need validate double values. When I TryParse .00 or ,00
return false
, but I allow this values must true.
This code not work correctly for this case
model = ,00 or .002 or ,789 or .12 ...
double number;
double.TryParse(model, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out number)
I don't think you can really do this safely. If you manage to get your system to handle both ways of defining
"."
and","
as the decimal separator and grouping separator, then you run into issues of ambiguity.For instance, what if
"1.001"
is passed in? What about"1,001"
? How do you know if either one really means "one-thousand-and one" or "one and one-thousandth"?You could loop all supported cultures:
or with LINQ:
old approach, too error-prone as asawyer has pointed out(
"1,000,000.00"
):You could replace commas with dots:When you use
NumberFormatInfo.InvariantInfo
you are forcing the parser to treat.
as the decimal separator and,
as the thousands separator. Sowould return true, but
would not.
If you want to support multiple formats you'd have to call
TryParse
for each format. There's not a way to try and parse a number that would be valid in an format because they could return different value."123,456"
could be interpreted two different ways depending on what culture you were using.