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

后端 未结 3 1339
一个人的身影
一个人的身影 2021-01-23 00:46

Currently if I do this

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

Then \'d\'

3条回答
  •  伪装坚强ぢ
    2021-01-23 01:50

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

提交回复
热议问题