Search code examples
c#csvcsvhelper

CsvHelper rounding a double to two places in maps


For mapping classes I have this.

public sealed class MyMap : ClassMap<MyBase>
{
    public MyMap()
    {
        Map(m => m.EventDate).Index(1);
        Map(m => Math.Round(m.Price,2)).Index(2);
    }
}

Is there a way that will work as this errors out when rounding. I know you can use custom converters but that seems like over kill just to truncate input to two decimal places.


Solution

  • Something like this?

    public sealed class MyMap : ClassMap<MyBase>
    {
        public MyMap()
        {
            Map(m => m.EventDate).Index(1);
            Map(m => m.Price).Index(2).Convert(row => Math.Round(row.Row.GetField<decimal>("Price"),2));
        }
    }