2008-10-19 70 views

回答

17
decimal one = 1000M; 
    decimal two = 12.5M; 

    Console.WriteLine(one.ToString("0.##")); 
    Console.WriteLine(two.ToString("0.##")); 
+1

它重要的是要記住,你的寶貝,包括儘可能多的跡象#只要你想,如果你用這個方法來顯示小數。 – AlexCuse 2008-10-19 15:45:49

20

更新繼user1676558

嘗試評論此:

decimal one = 1000M;  
decimal two = 12.5M;  
decimal three = 12.567M;  
Console.WriteLine(one.ToString("G"));  
Console.WriteLine(two.ToString("G")); 
Console.WriteLine(three.ToString("G")); 

對於十進制值,「G」格式說明符的默認精度爲29位,並且在省略精度時總是使用定點符號,所以這與「0。#### #########################」。

與「0。##」不同,它將顯示所有重要的小數位(十進制值不能超過29個小數位)。

「G29」格式說明符相似,但如果更加緊湊,可以使用科學記數法(請參閱Standard numeric format strings)。

這樣:

decimal d = 0.0000000000000000000012M; 
Console.WriteLine(d.ToString("G")); // Uses fixed-point notation 
Console.WriteLine(d.ToString("G29"); // Uses scientific notation 
+1

G29在某些情況下使用科學記數法(請參閱[MSDN文檔](https://msdn.microsoft.com/en-us/library/dwhawy9k(v = vs.110).aspx#Anchor_5)),0。 #############################就像G29一樣,不會回覆到科學記數法。 – user1676558 2017-02-16 00:33:31

相關問題