开发者

How do I make decimal.TryParse keep the trailing zeros?

开发者 https://www.devze.com 2023-02-16 23:43 出处:网络
Currently if I do this decimal d; temp = \"22.00\"; decimal.TryParse(temp, Num开发者_高级运维berStyles.Any,CultureInfo.InvariantCulture, out d);

Currently if I do this

decimal d;
temp = "22.00";
decimal.TryParse(temp, Num开发者_高级运维berStyles.Any,  CultureInfo.InvariantCulture, out d);

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

FYI I am using .net 4.0


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.

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?

For example:

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));
    }
}


I would say no and to just format the decimal with trailing zeros on display as 22 is still 22.00.


Then 'd' turns out as 22

And you expect to turn into what? The zeros are still there:

Console.WriteLine(d);
0

精彩评论

暂无评论...
验证码 换一张
取 消