[英]How do I make decimal.TryParse keep the trailing zeros?
目前,如果我這樣做
decimal d;
temp = "22.00";
decimal.TryParse(temp, NumberStyles.Any, CultureInfo.InvariantCulture, out d);
然后'd'變成22.有什么方法可以確保尾隨的零沒有被消滅掉?
僅供參考我正在使用.net 4.0
相同的代碼適用於我(如果我將輸入字符串更改為“22.000”,則顯示22.00和22.000),並且當您指定了不變文化時,它不應該依賴於我們各自的文化。
你之后如何檢查價值? 如果它在調試器中,我不會感到驚訝,如果這是責備...如果你打印出d.ToString()
這表示什么?
例如:
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。
然后'd'變成了22
你期望變成什么? 零仍然存在:
Console.WriteLine(d);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.