Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DecimalConstructor fails when parsing decimal string using scientific notation #8

@akamor

Description

@akamor

DecimalConstructor.cs is calling

Convert.ToDecimal(stringArg, CultureInfo.InvariantCulture)

So something like

System.Convert.ToDecimal("5E-1",CultureInfo.InvariantCulture)

Will throw a System.FormatException: Input string was not in a correct format. exception

This has bit us when using Microsoft.Spark (which takes this library as a dependency). We build C# UDFs that run in Spark which requires data to be serialized back and forth between the JVM and the CLR. In sparkland we have some decimal values that Spark decides to represent in scientific notation and when this gets pickled into the CLR we hit the above issue.

I did a bit of research and using something like

Decimal.Parse("5E-1, NumberStyles.AllowAny)

would work but is maybe too permissive. I think at a minimum we would need

Decimal.Parse("5E-1, NumberStyles.AllowLeadingSign | NumberStyles.DecimalPoint | NumberStyles.Exponent)

but there might be others.

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