简体   繁体   English

如何使decimal.TryParse保持尾随零?

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

Currently if I do this 目前,如果我这样做

decimal d;
temp = "22.00";
decimal.TryParse(temp, NumberStyles.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 ? 然后'd'变成22.有什么方法可以确保尾随的零没有被消灭掉?

FYI I am using .net 4.0 仅供参考我正在使用.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. 相同的代码适用于我(如果我将输入字符串更改为“22.000”,则显示22.00和22.000),并且当您指定了不变文化时,它不应该依赖于我们各自的文化。

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? 如果它在调试器中,我不会感到惊讶,如果这是责备...如果你打印出d.ToString()这表示什么?

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

我会说不,并且只显示带有尾随零的十进制格式,因为22仍然是22.00。

Then 'd' turns out as 22 然后'd'变成了22

And you expect to turn into what? 你期望变成什么? The zeros are still there: 零仍然存在:

Console.WriteLine(d);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM