如何让我decimal.TryParse保持尾随零? [英] How do I make decimal.TryParse keep the trailing zeros?

查看:373
本文介绍了如何让我decimal.TryParse保持尾随零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,如果我这样做

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

这篇关于如何让我decimal.TryParse保持尾随零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆