3

我正在使用FileHelpers创建固定长度的文件。在我的模型中,我有一个需要以 0000.00 格式输出的双精度。无论如何我可以用 FileHelpers 本身指定这个,还是我需要在创建模型时将我的模型更改为字符串并执行 .ToString(my_format) ?

4

1 回答 1

4

Have you tried using FieldConverters from the FileHelpers library?

Maybe something like this. This is untested, but it might get you on a working path:

using System;
using FileHelpers;

internal class MyDoubleConverter : ConverterBase
{
   public override string FieldToString(object from)
   {
      return ((double) from).ToString("0000.00");
   }
}

[FixedLengthRecord]
public class MyRecordType
{
   [FieldFixedLength(7)]
   [FieldConverter(typeof(MyDoubleConverter))]
   public double MyDouble;
}

Or this may work, and is even simpler:

[FixedLengthRecord]
public class MyRecordType
{
   [FieldFixedLength(7)]
   [FieldConverter(ConverterKind.Double, "0000.00")]
   public double MyDouble;
}

But I think that will enforce the 0000.00 for both reading and writing, and I'm wouldn't know whether that works for your scenario.

于 2011-05-26T21:07:25.707 回答