2013-12-23 81 views
3

首先,我希望我不會通過再次發佈相同的問題踩到任何腳趾。昨天我發佈了一個問題(https://stackoverflow.com/questions/20734071/trouble-when-using-parse),因爲我覺得我不清楚,所以暫時擱置。decimal.Parse不會在Visual Studio 2012中工作

現在我已經做了一些noob研究和.Parse問題似乎只發生在我使用decimal.Parse時。例如,這工作得很好:

private void button1_Click(object sender, EventArgs e) 
     { 
      string text = "500"; 
      int num = int.Parse(text); 
      MessageBox.Show(num.ToString()); 
     } 

但這:

private void button1_Click(object sender, EventArgs e) 
     { 

      decimal text = decimal.Parse(textBox1.Text); 
      decimal total = text * 2; 
      MessageBox.Show(total.ToString()); 

     } 

導致此錯誤: (在textBox1中的值是10.00) enter image description here

我曾嘗試重新安裝的Visual Studio但問題依然存在。

編輯:

System.FormatException was unhandled 
    HResult=-2146233033 
    Message=Input string was not in a correct format. 
    Source=mscorlib 
    StackTrace: 
     at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
     at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) 
     at System.Decimal.Parse(String s) 
     at WindowsFormsApplication4.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\XXX\Documents\Visual Studio 2012\Projects\WindowsFormsApplication4\Form1.cs:line 23 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at WindowsFormsApplication4.Program.Main() in c:\Users\XXX\Documents\Visual Studio 2012\Projects\WindowsFormsApplication4\Program.cs:line 19 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+3

您的區域設置是什麼? '(Console.WriteLine(CultureInfo.CurrentCulture.Name);' – Steve

+2

'textBox1.Text'中的值是什麼? –

+3

問題可能出在使用錯誤的小數點分隔符。像','而不是'.'。 –

回答

-1

decimal.Parse期望用戶提供有效的數字串即 「0.5」, 「1」 和類似的。 如果您嘗試傳遞無效字符串,即「」,「sdf」,那麼它會拋出異常。我建議你使用decimal.TryParse。 TryParse,返回boolean以指示成功轉換,並設置一個out變量。

此外,你應該考慮傳遞CultureInfo在重載之一,如不是所有的文化,被視爲小數點,因此

CultureInfo info = CultureInfo.GetCultureInfo("es-ES");//pass your culture 
decimal result; 
decimal.TryParse(textBox1.Text, NumberStyles.Any, info, out parsedValue)) 
+0

這個答案實際上沒有任何有用的東西。 OP似乎提供了一個有效的字符串,'TryParse'將嘗試與'Parse'相同類型的轉換,只是異常消耗。這最初導致我們沿着文化格式化路線。 –

+0

hey @AdamHouldsworth,OP直接解析textbox.Text,因此非常容易提供無效字符串,因此,他應該檢查其有效性,所以它似乎很有用,就像當你建議sql注入時有人嘗試從c#執行查詢# –

+0

'TryParse'是處理它的一種方式,但它不一定是正確的方式,它不一定會處理這個問題,這似乎是理解和迎合文化特定的格式。 'TryParse'很有用,但它與手動處理異常沒有什麼不同,目前並不直接涉及這個問題的細節。 –

1

因爲實際上傳遞一個字符串包含了一些我相信這是一個文化問題的失敗。這裏有一些提示:

  • 在一個不同的過載Parse方法
  • 的指定NumberStyles在不同的過載Parse方法的指定一個不變培養
  • 同時指定

參見例如如下:

decimal text = decimal.Parse(textBox1.Text, NumberStyles.Any, CultureInfo.InvariantCulture); 

And a提示與您的問題沒有直接關係...請始終使用TryParse使轉換更安全

+0

感謝您花時間,但恐怕答案是有點過頭了..我試圖取代我的第一行與你寫的bur我得到一個錯誤:numberstyles不存在於這種情況下.. – user2915962

+0

@ user2915962用'using'指令在文件的頂部添加'using System.Globalization;'。 – Thorarin

+0

你參考過'System.Globalization'命名空間嗎?這就是爲什麼你應該得到這個錯誤的唯一原因,這實際上是一個編譯錯誤 – Leo

相關問題