Skip to content

Nullable Decimals Don't Write Correctly with a 0;00 Format #17

@joseph-chew

Description

@joseph-chew

When trying to write to a file with a class that has a decimal? property and a format like 0;00 or 0;000, the values to the right of and including the decimal seem to be getting dropped.

Setup for Reproduction (.NET 6)

  1. Create a file that has a number value (one field should do)
    a) Example: 0000002340
  2. Create a class that has a decimal? property:
public record TestClass
{
    [FixedWidthLineField(Start = 1, Length = 10, Pad = '0', PadSide = PadSide.Left, Format = "0;00")]
    public decimal? TestProperty { get; init; } = default;
}
  1. Read the file into memory, then write the file again.
List<string> readData = File.ReadAllLines(@"PATH_TO_TEST_FILE").ToList();
TestClass testClass = new FixedWidthLinesProvider<TestClass>().Parse(readData).First();

// `testClass.TestProperty` should equal `23.4` after parsing the readData

var writeData = new FixedWidthLinesProvider<TestClass>().Write(new() { testClass });
File.WriteAllLines(@"PATH_TO_NEW_FILE");
  1. Check the newly created file and notice it's 0000000023 instead of the expected 0000002340

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions