首先,我希望我不會通過再次發佈相同的問題踩到任何腳趾。昨天我發佈了一個問題(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)
我曾嘗試重新安裝的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:
您的區域設置是什麼? '(Console.WriteLine(CultureInfo.CurrentCulture.Name);' – Steve
'textBox1.Text'中的值是什麼? –
問題可能出在使用錯誤的小數點分隔符。像','而不是'.'。 –