Skip to content
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

Open
lw-schick opened this issue Dec 27, 2016 · 3 comments
Open

Fixed length with datetime decimal #43

lw-schick opened this issue Dec 27, 2016 · 3 comments

Comments

@lw-schick
Copy link

lw-schick commented Dec 27, 2016

Hi,

I use fixed length for attributes like following:

[FixedLengthFile]
public class MyFile
{
	[FixedLengthField(1, 1)]
	public string LineType { get; set; }

	[FixedLengthField(2, 13, Padding = Padding.Right, PaddingChar = ' ')]
	public string FormatVersion { get; set; }

        // @ToDo: What to write here? .... i miss DecimalPlaces = 2 ...
        [FixedLengthField(15, 15, PaddingChar = '0')]
	public decimal Amount { get; set; }
}

How can I use fixed length with decimal values? - the specification allows 2 decimal places. I need to specify that somewhere...

@lw-schick
Copy link
Author

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 DecimalPlaces or something else. But that's just my view, maybe there is a reason to do this like that...

At least, I would wish to see this in a FAQ section or somewhere else. The parameter Convert is not documented.

@OpenSpacesAndPlaces
Copy link

@lw-schick

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".

@lw-schick
Copy link
Author

Yes, this source code worked for me.

The function ConvertToString is implemented because it's part of the ITypeConverter interface. In my implementation, I only needed an implementation for the type decimal so I didn't implement it e.g. for float values. I know when you would implement it into a library you should support more types...
I used it here just to give an example how it is currently possible.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants