不会保留。c#中的 decimal 类型在序列化为 json 字符串时,末尾的零会被自动去除,因为 json 数字规范不区分尾随零,且大多数序列化库(如 system.text.json)会将其转换为最简数值表示。

System.Text.Json 如何处理 decimal 的序列化?
System.Text.Json 默认将 decimal 序列化为 JSON 数字,并移除末尾零。例如,值 1.20m 会变成 1.2。这是由 JSON 标准决定的,而非 C# 特性。

using System;
using System.Text.Json;
class Program
{
static void Main()
{
decimal value = 1.20m;
string json = JsonSerializer.Serialize(value);
Console.WriteLine(json); // 输出: 1.2
}
}
运行上述代码,控制台输出为 1.2,验证了末尾零被移除。若需保留格式,需手动转为字符串或使用自定义转换器。
如何强制保留 decimal 的原始格式?
若需保留末尾零,可将 decimal 显式转为 string 再序列化,或使用 JsonSerializerOptions 配置自定义转换器。例如,使用 ToString() 可保留精度。
通过 Playwright + HTTP 实现网页搜索与内容抓取,无需 API 密钥,支持国内 Bing CN 与国际 DuckDuckGo,自动获取排名前列的页面内容。

using System;
using System.Text.Json;
class Program
{
static void Main()
{
decimal value = 1.20m;
var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(new { Value = value.ToString() }, options);
Console.WriteLine(json); // 输出: { "Value": "1.20" }
}
}
此方法输出 { "Value": "1.20" },保留了原始格式。注意,这会将值转为字符串,而非数字。
decimal 属性转 JSON 后,空值或零值如何处理?
空值(null)在 JSON 中表现为 null,零值(0.00m)序列化为 0。例如,0.00m 变为 0,无末尾零。若属性为 null,JSON 结果为 null,不触发格式化。










