Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/DbfDataReader/DbfRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ public class DbfRecord
private readonly int _recordLength;
private readonly byte[] _buffer;

public DbfRecord(DbfTable dbfTable)
private readonly DbfRecordConfiguration Config;

public DbfRecord(DbfTable dbfTable, DbfRecordConfiguration config = null)
{
if (config == null)
{
this.Config = DbfRecordConfiguration.GetDefault();
}
else {
this.Config = config;
}

_encoding = dbfTable.CurrentEncoding;
_recordLength = dbfTable.Header.RecordLength;
_buffer = new byte[_recordLength];
Expand Down Expand Up @@ -55,7 +65,13 @@ private IDbfValue CreateDbfValue(DbfColumn dbfColumn, DbfMemo memo)
value = new DbfValueLong(dbfColumn.Start, dbfColumn.Length);
break;
case DbfColumnType.Float:
value = new DbfValueFloat(dbfColumn.Start, dbfColumn.Length, dbfColumn.DecimalCount);
if (this.Config.ReadFloatsAsDecimals)
{
value = new DbfValueDecimal(dbfColumn.Start, dbfColumn.Length, dbfColumn.DecimalCount);
}
else {
value = new DbfValueFloat(dbfColumn.Start, dbfColumn.Length, dbfColumn.DecimalCount);
}
break;
case DbfColumnType.Currency:
value = new DbfValueCurrency(dbfColumn.Start, dbfColumn.Length, dbfColumn.DecimalCount);
Expand Down
25 changes: 25 additions & 0 deletions src/DbfDataReader/DbfRecordConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace DbfDataReader
{
/// <summary>
/// Class <c>DbfRecordConfiguration</c> provides a configuration for the <c>DbfRecord</c> class
/// </summary>
public class DbfRecordConfiguration
{
/// <summary>
/// Configuation that denotes floats from the database should be read as <c>decimal</c> in C#.
/// This is to mitigate precision issues where a number in the database is labeled as a float, but is greater than the size of a float in C#
/// </summary>
public bool ReadFloatsAsDecimals { get; set; }

/// <summary>
/// Provides a default configuration
/// </summary>
/// <returns>A <c>DbfRecordConfigration</c> object containing the default configuration</returns>
public static DbfRecordConfiguration GetDefault()
{
return new DbfRecordConfiguration {
ReadFloatsAsDecimals = false
};
}
}
}