使用FileHelpers填充零

时间:2015-07-31 15:24:38

标签: c# filehelpers leading-zero

我正在使用FileHelpers来创建NACHA文件。以下示例

许多属性都是带前导零的数字,因此它们已被定义为字符串。是否有一个属性可以填充Class属性中的前导零,类似于FieldTrim/TrimMode删除空格的方式?

[FixedLengthRecord()]
public class FileControlRecord
{
    [FieldFixedLength(1)]
    public int RecordTypeCode = 9; //Constant

    [FieldFixedLength(6)]
    public string BatchCount; //Numeric

    [FieldFixedLength(6)]
    public string BlockCount; //Numeric

    [FieldFixedLength(8)]
    public string EntryAddendaCount; //Numeric

    [FieldFixedLength(10)]
    public string EntryHash; //Numeric

    [FieldFixedLength(12)]
    public string TotalDebit; //$$$$$$$$$$cc

    [FieldFixedLength(12)]
    public string TotalCredit; //$$$$$$$$$$cc

    [FieldFixedLength(39)]
    [FieldNotInFile]
    public string RESERVED; //Blank
}

1 个答案:

答案 0 :(得分:6)

您必须将FieldAlign与填充字符:

一起使用
[FixedLengthRecord()]
public class FileControlRecord
{
    [FieldFixedLength(1)]
    public int RecordTypeCode = 9; //Constant

    [FieldFixedLength(6)]
    public string BatchCount; //Numeric

    [FieldFixedLength(6)]
    public string BlockCount; //Numeric

    [FieldFixedLength(8)]
    public string EntryAddendaCount; //Numeric

    [FieldFixedLength(10)]
    public string EntryHash; //Numeric

    [FieldFixedLength(12)]
    [FieldAlign(AlignMode.Right, '$')]
    public string TotalDebit; //$$$$$$$$$$cc

    [FieldFixedLength(12)]
    [FieldAlign(AlignMode.Right, '$')]
    public string TotalCredit; //$$$$$$$$$$cc

    [FieldFixedLength(39)]
    public string RESERVED; //Blank
}

PS:我建议您使用该库的最新版本:

https://github.com/MarcosMeli/FileHelpers/releases/latest

或通过NuGet https://www.nuget.org/packages/FileHelpers/