decimal.Parse不会在visual studio 2012中工作

时间:2013-12-23 10:30:31

标签: c# parsing

首先,我希望通过再次发布相同的问题,我不会踩到任何脚趾。昨天我发布了一个被搁置的问题(https://stackoverflow.com/questions/20734071/trouble-when-using-parse)因为我觉得我不清楚。

现在我一直在做一些菜鸟研究,当我使用decimal.Parse时,似乎只会出现.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: 

2 个答案:

答案 0 :(得分:1)

由于您实际传递的是包含数字的字符串,因此我认为它未能解决文化问题。以下是一些提示:

  • NumberStyles方法
  • 的不同重叠中指定Parse
  • Parse方法
  • 的不同重载中指定不变文化
  • 同时指定

见下面的例子:

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

提示与您的问题没有直接关系...始终使用TryParse使转换更安全

答案 1 :(得分:-1)

decimal.Parse期望您提供有效的数字字符串,即“0.5”,“1”等。 如果你试图传递一个无效的字符串,即“”,“sdf”,那么它将抛出异常。

我建议您使用decimal.TryParse。 TryParse,返回boolen表示转换成功,并设置out变量。

另外,您应该考虑在其中一个重载中传递CultureInfo,因为并非所有文化都被视为小数点,因此

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