DbfDataReader icon indicating copy to clipboard operation
DbfDataReader copied to clipboard

Added a configuration object for the DbfRecord class

Open Dmbob opened this issue 2 years ago • 0 comments

It is possible to run into issues where a value in a dbf file is has a type of Float, but is larger than a float in C#.

This would cause precision issues when the value is passed into the float.TryParse method.

See the below example:

var numberAsString = "123456.78";
float.TryParse(numberAsString, out var numberAsFloat); // numberAsFloat will be 123456.781 here

// Using ToString will round it back to 123456.78
Console.WriteLine(numberAsFloat.ToString());

It's worth noting that calling ToString on the float with no parameters would round to the nearest hundredth and coincidentally show the value as what it is supposed to be.

In order to mitigate this, a configuration class was created that can be passed into the DbfRecord class. This configuration currently contains one property (ReadFloatsAsDecimals) that will tell the CreateDbfValue method to use the DbfValueDecimal class for floats instead of the DbfValueFloat class.

The configuration that gets provided is an optional parameter on the DbfRecord class and provides a default configuration that matches the current expected behavior.

Dmbob avatar Sep 01 '21 16:09 Dmbob