代码背后的舍入代码错误 [英] Rounding code error in code behind

查看:97
本文介绍了代码背后的舍入代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络表单上有两个文本框。 Thextbox1有一个文本框文本更改,textbox2显示答案。我试图让textbx2在文本框文本更改时舍入到最接近的整数。我在舍入代码行上有错误。以下是代码背后的代码:



I have two textboxes on a web form. Thextbox1 has a textbox text change and textbox2 shows the answer. I am trying to get textbx2 to round to the nearest whole number on textbox text change. I am having an error on the rounding code line. Here is the code behind:

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        int w = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
        int v = 0;
        TextBox2.Text = Convert.ToString(w + v);
        RangeValidatorLY1.Validate();
        TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text), 2);
        TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));
        TextBox3.Focus();

    }





我缺少什么?



What am I missing?

推荐答案

就行:TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text),2);,

Math.Round(Convert.ToString(TextBox2) .Text),2);在它下面有一个红线。错误说:'System.Math.Round(decimal)'的最佳重载方法匹配有一些无效的参数。第二个错误说:参数1:无法转换'string'到'decimal'。我只是想绕到最接近的整数。






um 。您是否习惯将字符串转换为其他字符串? :笑:

可能,你的意思是:

"on the line: TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text), 2);,
Math.Round(Convert.ToString(TextBox2.Text), 2); has a red line under it. Error saying: The best overloaded method match for 'System.Math.Round(decimal)' has some invalid arguments. And the second error says: Argument 1: cannot convert from 'string' to 'decimal'. I am just trying to round to the nearest whole number."



Um. Are you in the habit of converting strings into other strings? :laugh:
Possibly, what you meant to say was:
TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();





但是......因为你首先要添加两个整数来产生字符串值,所以舍入是(通过完全没有任何差别......:笑:



很抱歉,但整个代码没有多大意义。你究竟想做什么?



But... since you add two integers to produce the string value in the first place, rounding is (by definition) not going to make any difference at all... :laugh:

Sorry, but that code as a whole doesn't make a lot of sense. What are you actually trying to do?


显然,你不需要舍入,但你只需要在文本框中显示舍入值。这不一样。明确的舍入很少需要并且有潜在危险,因为你可以使用舍入值作为中间结果,这可能导致准确性的损失。



你是什么想要使用正确的字符串格式解决。请参阅:

http ://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/26etazsy%28v=vs.110%29.aspx> http://msdn.microsoft.com/en-us/library/26etazsy %28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx [ ^ ],

http:// msdn .microsoft.com / zh-CN / library / 0c899ak8%28v = vs.110%29.aspx [ ^ ]。



然而,一个合理的案例使用 Math.Round 将从浮点值获得一个整数值,但我不确定这是你所需要的。



此外,为您的目的使用转换是不好的。这是解析,所以最好使用 double.TryParse ,它不会抛出异常但返回布尔成功结果。 (您也可以使用 double.Parse ,但是准备好处理异常;无论您如何过滤输入,都可以在文本框中输入无效的double值)。



-SA
Apparently, you don't need rounding, but you just need presenting rounded value in a text box. This is not the same. Explicit rounding is quite rarely needed and is potentially dangerous, because you can, be any chance, use rounded value as intermediate result which can cause loss of accuracy.

What you want should be resolved using proper string formatting. Please see:
http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/26etazsy%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx[^].

However, one reasonable case to use Math.Round would be getting an integer value from floating-point one, but I'm not sure this is what you need in your case.

Besides, it's bad to use Convert for your purpose. This is parsing, so it's better to use double.TryParse, which doesn't throw exception but return Boolean success result. (You can use double.Parse, too, but be ready to handle exceptions; invalid double value can always be entered in a text box, no matter how hard you filter the input).

—SA


//声明ceilingvalue变量

double ceilingvalue = Math.Ceiling(Convert.ToDouble(TextBox1.Text));

Label1.Text = ceilingvalue.ToString(#);
//Declare ceilingvalue variable
double ceilingvalue = Math.Ceiling(Convert.ToDouble(TextBox1.Text));
Label1.Text = ceilingvalue.ToString("#");


这篇关于代码背后的舍入代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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