I’m trying to implement a numeric converter that takes an object and converts it to the required type.
I found a problem though: when I test “1,5” with the “en-US” culture Convert.ToSingle and Convert.Double simply returns “15” instead of throwing an exception that it cannot convert this value, which I would actually expect because “1,5” is invalid.
Does anyone know why?
No,
"1,5"is perfectly valid. Similar to how"1,500"parses to1500. Basically, the group separators (,in en-US) are simply ignored when parsing.If you don’t want it to do this, specify the
NumberStyles:note that this is a
[Flags]enum, and importantly I’m not includingAllowThousands. Another approach could be:which allows everything except group separators. Of course,
"1,500"will now fail.