Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added samples/xlsx/TestIssue409.xlsx
Binary file not shown.
Binary file added samples/xlsx/TestIssue881.xlsx
Binary file not shown.
13 changes: 11 additions & 2 deletions src/MiniExcel/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,17 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
}
else if (pInfo.ExcludeNullableType == typeof(double)) // && (!Regex.IsMatch(itemValue.ToString(), @"^-?\d+(\.\d+)?([eE][-+]?\d+)?$") || itemValue.ToString().Trim().Equals("NaN")))
{
var invariantString = Convert.ToString(itemValue, CultureInfo.InvariantCulture);
newValue = double.TryParse(invariantString, NumberStyles.Any, CultureInfo.InvariantCulture, out var value) ? value : double.NaN;
if (double.TryParse(Convert.ToString(itemValue, config.Culture), NumberStyles.Any, config.Culture, out var doubleValue))
{
newValue = doubleValue;
}
else
{
var invariantString = Convert.ToString(itemValue, CultureInfo.InvariantCulture);
newValue = double.TryParse(invariantString, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)
? value
: throw new InvalidCastException($"Value \"{itemValue}\" cannot be cast to double");
}
Comment on lines +202 to +212
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic here is correct, but the nested structure combined with a ternary expression using throw can be a bit dense and harder to read. Refactoring this to a more standard if-else structure would improve code clarity and maintainability without altering the functionality.

                if (double.TryParse(Convert.ToString(itemValue, config.Culture), NumberStyles.Any, config.Culture, out var doubleValue))
                {
                    newValue = doubleValue;
                }
                else
                {
                    var invariantString = Convert.ToString(itemValue, CultureInfo.InvariantCulture);
                    if (double.TryParse(invariantString, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
                    {
                        newValue = value;
                    }
                    else
                    {
                        throw new InvalidCastException($"Value \"{itemValue}\" cannot be cast to double");
                    }
                }

}
else if (pInfo.ExcludeNullableType == typeof(bool))
{
Expand Down
Loading