Document unit-test / theory-data impact of .NET 11 double→decimal conversion
维护者通常 1 天内回复
评估
- 难度
- 1/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 88/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
从 docs/core/compatibility/core-libraries/11/decimal-biginteger-floating-point-conversions.md 入手,检查现有的 Recommended action 部分;相关的 .NET 11 库页面是 docs/core/whats-new/dotnet-11/libraries.md。添加一个简洁的单元测试影响小节,包含 issue 中描述的 xUnit 和 NUnit 指导及示例。完成标准是页面解释浮点测试数据如何转换为 decimal,并展示能够保留 decimal 的替代方案。
由索引模型根据 Issue 内容生成。
描述
Type of issue
- Content issue (typos, broken links, missing/outdated content)
- Product issue (bug in docs tooling/platform)
Location
- https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/11/decimal-biginteger-floating-point-conversions
- Source:
docs/core/compatibility/core-libraries/11/decimal-biginteger-floating-point-conversions.md - Also referenced from
docs/core/whats-new/dotnet-11/libraries.md
Description
The breaking-change page correctly documents that float/double → decimal is now correctly rounded (exact binary value), and recommends decimal literals (1.23m) instead of casting double literals.
What is easy to miss in migration: unit-test theory / inline data often injects floating literals as double, then binds or casts them to decimal parameters. That path was “quietly OK” on .NET 10 (15-digit-style conversion) and fails equality / validation on .NET 11 even when production code already uses decimal end-to-end.
xUnit
// Compiles: 0.1 is a double attribute argument
[Theory]
[InlineData(0.1)]
public void Rate(decimal rate)
{
// Under .NET 10: rate often equals 0.1m
// Under .NET 11: rate == 0.1000000000000000055511151231m → assert fails
Assert.Equal(0.1m, rate);
}
// Does NOT compile: decimal is not a valid C# attribute argument type
// [InlineData(0.1m)]
Workaround that keeps real decimals: MemberData / ClassData / TheoryData<decimal> with 0.1m values (not attribute args).
NUnit
// Non-m literals become double, then convert to decimal param
[TestCase(0.1)]
public void Rate(decimal rate) { ... }
// Prefer:
[TestCase("0.1")]
public void Rate(decimal rate) { ... } // parsed as decimal
Why document this on the BC page
- The failure mode is often tests only, so teams can think production is broken when CI is.
- It is not obvious that
InlineData(0.1)+decimalis a double conversion (C# forbids0.1min attributes). - Same BC already says “don’t assume a binary FP literal and a decimal literal with the same source text represent the same value” — a short “Impact on unit tests” subsection with xUnit/NUnit examples would catch a large class of migration failures.
Suggested addition (sketch)
Under Recommended action, add something like:
Unit tests (xUnit / NUnit / similar)
Theory/case attributes cannot take decimal constants in C#. Non-integer numeric attribute args are double/float. Binding those to decimal parameters uses the same conversion as (decimal)double and therefore changes under this BC. Prefer:
- xUnit:
TheoryData/MemberDatawithdecimalvalues (0.1m), or string data +decimal.Parse - NUnit:
[TestCase("0.1")]intodecimal, or explicit0.1monly outside attributes
Page URL
Related
- Runtime BC is intentional; this is a docs request only (no ask to revert conversion).
- xUnit docs request: https://github.com/xunit/xunit/issues/3658
- xUnit attribute limitation is language-level (
decimalnot allowed in attribute arguments).
- 主要语言
- 没有语言数据
- 星标
- 4.8k
- 派生
- 6.1k
- 平均合并
- 20 小时 42 分钟
- 30 天内合并 PR
- 199
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/docs 的其他 Issue
-
:watch: Not Triaged dotnet-fundamentals/svc
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
dotnet-fundamentals/svc install/subsvc
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
Upgrade from `net9.0` to LTS (or STS) image tag可能已有人在做 @ShyneChikwapulo 于 8 天前认领。 未关闭dotnet-target-version
难度 1/5 1 小时以内 新手友好度 85/100
dotnet/docs#56293 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Documentation for dotnet sln add doesn't mention the --include-references flag可能已有人在做 @BartoszKlonowski 于 8 天前认领。 未关闭dotnet-fundamentals/svc
难度 1/5 1 小时以内 新手友好度 92/100
dotnet/docs#56289 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Add more to the documentation可能已有人在做 @adarsh6980 于 27 天前认领。 未关闭dotnet-fsharp/svc help wanted
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
cran-task-views/Survival#76 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
type:docs
难度 2/5 1-3 小时 新手友好度 76/100
sandtable-ai/sandtable#300 ·
维护者通常 1 天内回复
-
content good first issue
难度 2/5 1-3 小时 新手友好度 78/100