2016-03-29 44 views
0

我試圖將負數字符串如"-23.27"轉換爲小數點值,並且問題有時會在圓括號內得到負值(如"(23.27)")。如何將負數字符串轉換爲十進制值?

我寫了控制括號的代碼並以負格式獲取小數點值,但是當使用"-23.27"運行相同的代碼時,它會返回輸入字符串的格式不正確。這是我的代碼。任何幫助,將不勝感激。

decimal ValueN = (decimal.Parse("-23.27", 
        System.Globalization.NumberStyles.AllowParentheses | 
        System.Globalization.NumberStyles.AllowLeadingWhite | 
        System.Globalization.NumberStyles.AllowTrailingWhite | 
        System.Globalization.NumberStyles.AllowThousands)); 
+0

_NumberStyles.Number | NumberStyles.AllowParentheses_ – Steve

+3

你忘了一對夫婦(領先的符號和小數)。只需使用Number | AllowParentheses。 –

回答

7

你錯過了AllowLeadingSign標誌

decimal ValueN= (decimal.Parse("-23.27", 
       System.Globalization.NumberStyles.AllowParentheses | 
       System.Globalization.NumberStyles.AllowLeadingWhite | 
       System.Globalization.NumberStyles.AllowTrailingWhite | 
       System.Globalization.NumberStyles.AllowThousands | 
       System.Globalization.NumberStyles.AllowDecimalPoint | 
       System.Globalization.NumberStyles.AllowLeadingSign)); 
+0

它工作。我不知道我是如何錯過的。非常感謝你 –

+2

作爲一個側面提示,當我解析'-23.27'時,我需要'System.Globalization.NumberStyles.AllowDecimalPoint',否則我得到一個異常。 –

+0

好,趕快加入吧。 –

相關問題