Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Document unit-test / theory-data impact of .NET 11 double→decimal conversion

未关闭 适合新手
#56,487 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@SkyDevLab 已经在做这个了。

开始于 2026年10月21日。

  • #56516 来自 @SkyDevLab —— 未关闭

评估

难度
1/5
预计耗时
1-3 小时
新手友好度
88/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
documentation

调研方向

从 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 内容生成。

描述

:watch: Not Triaged dotnet-fundamentals/svc
Type of issue
  • Content issue (typos, broken links, missing/outdated content)
  • Product issue (bug in docs tooling/platform)
Location
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) + decimal is a double conversion (C# forbids 0.1m in 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 / MemberData with decimal values (0.1m), or string data + decimal.Parse
  • NUnit: [TestCase("0.1")] into decimal, or explicit 0.1m only outside attributes
Page URL

https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/11/decimal-biginteger-floating-point-conversions

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 (decimal not allowed in attribute arguments).

Related Issues

主要语言
没有语言数据
星标
4.8k
派生
6.1k
平均合并
20 小时 42 分钟
30 天内合并 PR
199

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/docs 的其他 Issue

查看 dotnet/docs 的全部 Issue

相似的 Issue

更多 Documentation Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。