Skip to content

TypeConverter .ConvertFromString with CultureInfo culture fails to convert decimals with grouping (thousands) char #12154

@PlofreCoder

Description

@PlofreCoder

Type of issue

Code doesn't work

Description

While converting german decimals from a textfile to decimals of floats, this function of the TypeConverter (TC) seems to take offense in the grouping char of the given culture (here de-DE) and results in a runtime error.
Note that when parsing text that contains a decimal, but no thousands separator, the conversions all succeed.
Code tested in LinqPad 5.5 and VS2029 with .NET 4.6.1

(sorry for the german text ;):
10.123,45 ist kein gültiger Wert für Double.
Here is the sample code from LinqPad (hence the Dump()-calls) to produce this error:

`var s = "10.123,45";
var t = typeof(double);
var tc = System.ComponentModel.TypeDescriptor.GetConverter(t);
var cult = System.Threading.Thread.CurrentThread.CurrentCulture;

decimal dec = decimal.Parse( s); //ok
dec.Dump();
var d = Convert.ChangeType(s, t, cult); //ok
dec.Dump();
var dc = tc.ConvertFromString(null, cult, s); //fails
dc.Dump();
var ds = tc.ConvertFromString(s);//fails
ds.Dump();`

After a long conversation with AIs it turns out that only using:
var d = Convert.ChangeType(s, t, cult); //ok
produces a working conversion. The explicit Parse call is out of the question as the type being used can change at runtime.

As my code does a call to TypeConverter.CanConvertFrom(targetType), I was expecting to also use the TypeConverter to actually convert the text into the desired type.

The explanation for this strange behavior by the AI was:
`
Because a TypeConverter is not primarily a “data parsing” API; it is a component-model / design-time conversion API, and the built-in numeric converters are intentionally conservative about what they accept as “canonical” text.
Concretely, DecimalConverter (and friends) parse using a restricted NumberStyles that does not include AllowThousands. That means:
It will accept forms like 123 or 123,45 (with the supplied culture), but it will reject grouped forms like 123.456,78, even though they are valid in de-DE.

The culture you pass only tells it which decimal separator and sign symbols to use; it does not change the converter’s choice of what syntactic forms are permitted. That choice is hard-coded in the converter implementation.
`

I think this should be noted in the documentation and a Link added to ChangeType

And maybe if somebody can reproduce this problem, they can raise this to get a fix?? I'm new here and have no idea how to raise bugs (which I think it is) here.

[Enter feedback here]

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.convertfromstring?view=netframework-4.6.1&devlangs=csharp&f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.ComponentModel.TypeConverter.ConvertFromString)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%3Dv4.6.1)%3Bk(DevLang-csharp)%26rd%3Dtrue

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.ComponentModel/TypeConverter.xml

Document Version Independent Id

c197d116-9388-6449-5406-7a367dc1523d

Platform Id

e824dce0-8a22-a1e8-85f8-b059c9eb30df

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area ownersuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions