-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed length with datetime decimal #43
Comments
I discovered the code and I found the following solution: [FixedLengthFile]
public class MyFile
{
[FixedLengthField(1, 1)]
public string LineType { get; set; }
[FixedLengthField(2, 13, Padding = Padding.Right, PaddingChar = ' ')]
public string FormatVersion { get; set; }
[FixedLengthField(15, 15, PaddingChar = '0', Converter = typeof(ConverterDecimalTwoDigs))]
public decimal Amount { get; set; }
}
internal class ConverterDecimalTwoDigs : ITypeConverter
{
public bool CanConvertFrom(Type type)
{
return type == typeof(string);
}
public bool CanConvertTo(Type type)
{
return type == typeof(decimal);
}
public string ConvertToString(object source)
{
if (source.GetType() != typeof(decimal))
throw new ArgumentException("Not expected type", nameof(source));
return ((decimal)source).ToString("F2", CultureInfo.GetCultureInfo("de-DE"));
}
public object ConvertFromString(string source)
{
return decimal.Parse(source, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.GetCultureInfo("de-DE"));
}
} In my eyes, that is ugly. Would be nice to have another possibility to do this f.e. with At least, I would wish to see this in a FAQ section or somewhere else. The parameter |
Did you see this working, or is this just a code mock-out? In my case "ConvertToString" is never fired, and looking at the source code there doesn't appear to be any calls to "ConvertToString". |
Yes, this source code worked for me. The function The point of this issue is that there is no support to define the number of decimal places via a parameter, so I would have to build an extra class when I need support for a fixed-point value with 5 decimal places after the point (e.g. 12345.67892). |
Hi,
I use fixed length for attributes like following:
How can I use fixed length with decimal values? - the specification allows 2 decimal places. I need to specify that somewhere...
The text was updated successfully, but these errors were encountered: