使用 FileHelpers 填充零

Padding zeros with FileHelpers

我正在使用 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
}

您必须将 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/