c# - decimal.Parse 无法在 visual studio 2012 中使用

标签 c# parsing

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

现在我一直在做一些菜鸟研究,.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: 

最佳答案

由于您实际上传递的是一个包含数字的字符串,我认为这是文化问题。以下是一些提示:

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

请看下面的例子:

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

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

关于c# - decimal.Parse 无法在 visual studio 2012 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741606/

相关文章:

c# - Xml 文件未复制到测试输出目录

c# - 通用 Windows 应用程序导航

python - 如何在 Python 中解析人们的名字和姓氏?

regex - 用 bash 解析字符串并提取数字

python - 使用 Python 的 HTMLParser 获取适合定义的 CSS 选择器的标签之间的数据

c# - IJSRuntime 忽略服务器端 blazor 项目中的自定义 json 序列化程序

c# - 是否有等效于在 F# 中创建 C# 隐式运算符?

c# - CSVHelper:使用 IndexAttribute 写出一个空字段

java - 在没有POJO的情况下用Gson解析Json?

python - 创建 LL(1) 语法时出现问题