FileHelpers icon indicating copy to clipboard operation
FileHelpers copied to clipboard

.NET formatted Numeric Converter?

Open devmaestro1 opened this issue 4 years ago • 0 comments

Hi - first up, thanks for this excellent library!

I have been looking through the code (and StackOverflow) to see if there was some way to format a number using the standard .NET format specifiers.

For example, I would have expected that if you wanted to use a decimal field, you could also specify the output/input format like "0.00".

It seems that this is possible via a CustomConverter, however wouldn't it be desirable to have a built-in converter kind (ConverterKind.Numeric or ConverterKind.Generic) that allows you to do this? I imagine the part of the code would look as follows:

  ```
   public class NumberFormatter : ConverterBase
    {
        public NumberFormatter()
        {
            NumberFormat = string.Empty;
            AdditionalFormat = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="numberFormat"></param>
        /// <param name="additionalFormat"></param>
        public NumberFormatter(string numberFormat, string additionalFormat)
        {
            NumberFormat = numberFormat;
            AdditionalFormat = additionalFormat;
        }

        /// <summary>
        /// The C# style numeric expression
        /// </summary>
        public string NumberFormat { get; set; }
        /// <summary>
        /// Currently unused
        /// </summary>
        public string AdditionalFormat { get; set; }

        public override object FieldToString(object val)
        {
            return val.ToString(NumberFormat, CultureInfo.InvariantCulture);
        }
    }

Have I missed something or does this feature not exist?

P.S. I unfortunately cannot use the custom converter as I am creating a ClassFromString which means I cannot reference the custom type without injecting the class source. It's a potential solution, just not an ideal one.

devmaestro1 avatar Jul 16 '21 11:07 devmaestro1