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
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup .NET 8
- name: Setup .NET 10
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x

- name: Setup .NET 10
- name: Setup .NET 11
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
dotnet-version: 11.0.x

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Setup .NET 11
uses: actions/setup-dotnet@v4
with:
dotnet-version: 11.0.x
- name: Restore dependencies
run: dotnet restore
env:
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/MiniExcelLibs.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;net461;netstandard2.0;net8.0;net9.0;net10.0</TargetFrameworks>
<Version>1.46.1</Version>
<LangVersion>14</LangVersion>
<TargetFrameworks>net45;net461;netstandard2.0;net8.0;net9.0;net10.0;net11.0</TargetFrameworks>
<Version>1.47.0</Version>
<LangVersion>15</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MiniExcelLibs</RootNamespace>
</PropertyGroup>
Expand Down
51 changes: 49 additions & 2 deletions src/MiniExcel/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ 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")))
{
if (double.TryParse(Convert.ToString(itemValue, config.Culture), NumberStyles.Any, config.Culture, out var doubleValue))
if (itemValue is double f64)
{
newValue = f64;
}
else if (double.TryParse(Convert.ToString(itemValue, config.Culture), NumberStyles.Any, config.Culture, out var doubleValue))
{
newValue = doubleValue;
}
Expand All @@ -208,9 +212,36 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
: throw new InvalidCastException($"Value \"{itemValue}\" cannot be cast to double");
}
}
else if (pInfo.ExcludeNullableType == typeof(decimal))
{
if (itemValue is decimal dec)
{
newValue = dec;
}
else if (itemValue is double f64)
{
#if NET11_0_OR_GREATER
newValue = ConvertToDecimalWithLegacyApproximation(f64);
#else
newValue = Convert.ToDecimal(f64, CultureInfo.InvariantCulture);
#endif
}
else if (decimal.TryParse(Convert.ToString(itemValue, config.Culture), NumberStyles.Any, config.Culture, out var decimalValue))
{
newValue = decimalValue;
}
else
{
var invariantString = Convert.ToString(itemValue, CultureInfo.InvariantCulture);
newValue = decimal.TryParse(invariantString, NumberStyles.Any, CultureInfo.InvariantCulture,
out var value)
? value
: throw new InvalidCastException($"Value \"{itemValue}\" cannot be cast to decimal");
}
}
else if (pInfo.ExcludeNullableType == typeof(bool))
{
var vs = itemValue.ToString();
var vs = itemValue?.ToString();
if (vs == "1")
newValue = true;
else if (vs == "0")
Expand Down Expand Up @@ -245,6 +276,22 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
return newValue;
}

#if NET11_0_OR_GREATER
/// <summary>Converts the specified double value to decimal using the same approximation applied by versions of .NET no greater than 10.</summary>
/// <remarks>
/// In .NET 11 conversion APIs such as <see cref="Decimal(double)" /> and <see cref="Convert.ToDecimal(double)" /> among others
/// have been changed to approximate the resulting value more accurately. Unfortunately this is a significant breaking change for us
/// as we relied upon this truncation to discard unwanted decimal digits when mapping doubles (our default numeric type) to decimals.
/// This method is therefore a workaround to ensure backwards compatibility with previous versions of the library.
/// </remarks>
private static decimal ConvertToDecimalWithLegacyApproximation(double value)
{
Span<char> text = stackalloc char[32];
value.TryFormat(text, out var length, "G15", CultureInfo.InvariantCulture);
return decimal.Parse(text[..length], NumberStyles.Float, CultureInfo.InvariantCulture);
}
#endif

#if !NET45
public static bool IsAsyncEnumerable(this Type type, out Type genericArgument)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcelTests/MiniExcelTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
Loading