查询MySQL中的乘法c# [英] Multiplication in query MySQL c#

查看:86
本文介绍了查询MySQL中的乘法c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我试图通过乘以标签和组合框中的值来计算如何计算订单。这就是我到目前为止



Hi everyone. I am trying to work out how to calculate a order by multiplying the values in a label and a combobox. This is what I''ve got so far

decimal price= Convert.ToDecimal(label1.Content);
          decimal quantity = Decimal.Parse(comboBox3.SelectedItem.ToString());
          decimal total = quantity * price;

          label2.Content = total;





错误:输入字符串不是格式正确。



有人可以就此事给我任何建议或给我一个更好的方法吗?



ERROR: Input string was not in a correct format.

Can anybody give me any advice on this matter or give me a better way of doing this?

推荐答案

我相信它很可能在comboBox3.SelectedItem.ToString()上出错。通常,在调用.ToString函数之前,您必须根据要解析的内容调用所选项的值或文本。



因此它应该看起来像comboBox3 .SelectedItem.Value.ToString()



**仅供参考我不知道你的组合框使用的控件类型是否只是一个名字您选择了或者它是否是一个组合框,因此呼叫可能需要不同的语法,然后我根据所使用的控件说明了上述内容。如果这对你没有帮助,请告诉我,我可以看看我能做些什么。





我是假设您能够正确访问该值。如果你不能告诉我。



从你上面的评论我看到你试图解析字符串9,000本身会导致错误。正如上面的评论者所说,文化信息规定了什么被部分地视为可接受的输入。



您也可以指定数字样式。管道分隔符|放在NumberStyles之间意味着将使用它们。 http://msdn.microsoft.com/en-us/library/system.globalization。 numberstyles.aspx [ ^ ]有一个列表,列出了所有可以接受为有效输入的数字样式。





以下代码将处理字符串9,000







I believe it is most likely erroring on comboBox3.SelectedItem.ToString(). Typically you have to call a selected item''s Value or Text depending on what you want to parse before calling the .ToString function.

So it should look like comboBox3.SelectedItem.Value.ToString()

**FYI I don''t know what type of control is being used for your combo box whether that is just a name you chose or if it is a combo box so the call may require a different syntax then what I''ve stated above depending on the control used. If this doesn''t help you, please let me know and I can see what I can do.


I''m going to assume that you are able to access the value correctly. If you can''t please let me know.

From your comments above I saw that you are trying to parse the string 9,000 which by itself will cause an error. As the above commenter is eluding to culture info specifys what is partically viewed as acceptable input.

You can also specify number styles as well. A pipe delimiter | placed between NumberStyles means that both will be used. http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[^] has a list of all the various number styles that can be accepted as valid input.


The below code will process the string 9,000



using System.Globalization;

decimal quantity = decimal.Parse("9,000", NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                            CultureInfo.GetCultureInfo("en-US").NumberFormat);


这篇关于查询MySQL中的乘法c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆