JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

After type conversion, still a ReaderException is thrown #2186

Closed AGuyCalledGerald closed 10 months ago

AGuyCalledGerald commented 10 months ago

Post this as a bug because the behaviour seems inconsistent.

I try to read a csv file that contains decimals with the following format: 120,000.00 Comma is the thousands separator, point is decimal separator. The csvreader uses the culture de-CH.

public class MyFile { public decimal? MyField { get; set; } }

public MyFileMap : ClassMap<MyFile> { public MyFileMap() { Map(x => x.MyField).Name("My Field").TypeConverterOption.NumberStyles(System.Globalization.NumberStyles.Number).TypeConverter<DecimalStringReplacer>(); //custom replacer removes the comma } }

var records = csvReader.GetRecords<MyFile>() //csvReader uses CultureInfo "de-CH". Raises ReaderException

I want to convert the strings with the decimals to nullable decimals. But the format is not correct for de-CH, which expects a different thousands separator, so a TypeConverterException is thrown. I now used a custom type converter which removes the comma, which gets rid of the TypeConverterException. But now a ReaderException is thrown, complaining that it cannot convert the string to decimal.

Should not the custom type converter resolve the conversion problem? If not, what can I do to resolve the issue?

CsvHelper version 15.0.8, Stack trace at CsvHelper.CsvReader.d__63`1.MoveNext()