如何让我decimal.TryParse保持尾随零?让我、decimal、TryParse

2023-09-04 22:33:32 作者:逆境中征服つ

目前,如果我这样做

decimal d;
temp = "22.00";
decimal.TryParse(temp, NumberStyles.Any,  CultureInfo.InvariantCulture, out d);

然后'D'原来为22。有什么办法,我可以保证尾随零没有得到消灭了?

Then 'd' turns out as 22. Is there any way I can ensure that trailing zeros don't get wiped out ?

FYI我使用.NET 4.0

FYI I am using .net 4.0

推荐答案

同样的code对我的作品(显示22.00,和22.000如果我改变输入字符串为22.000),并为您指定固定区域性不应该依赖的我们的各自的文化。

The same code works for me (displaying 22.00, and 22.000 if I change the input string to "22.000"), and as you've specified the invariant culture it shouldn't depend on our respective cultures.

你是如何检查的价值之后?如果是在调试器中,我也不会感到惊讶,如果是应该受到谴责......如果你打印出 d.ToString()那有什么节目?

How are you examining the value afterwards? If it's in the debugger, I wouldn't be surprised if that were to blame... if you print out d.ToString() what does that show?

例如:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        decimal d;
        decimal.TryParse("22.00", NumberStyles.Any,
                         CultureInfo.InvariantCulture, out d);

        // This prints out 22.00
        Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
    }
}