如何全局修改 C# decimal 的 tostring 去除小数尾数0?

我知道以下能做到,但如何覆盖所有 decimal tostring?

    public static decimal Normalize(this decimal value)
    {
        return value / 1.000000000000000000000000000000000m;
    }
阅读 2.3k
1 个回答
public static class DecimalExtensions
{
    public static string ToNormalizedString(this decimal value)
    {
        decimal normalized = value / 1.000000000000000000000000000000000m;
        return normalized.ToString("G29"); // "G29" format specifier removes trailing zeros
    }
}
logo
Microsoft
子站问答
访问
宣传栏