尝试舍入到最接近的整数,但舍入不起作用。为什么? [英] Trying to Round to the nearest whole number but Rounding is not working. Why?

查看:83
本文介绍了尝试舍入到最接近的整数,但舍入不起作用。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码应该舍入到最接近的整数,当我测试它时,数字不是四舍五入。当我在TextBox1中输入1523时,该数字被分成12并且TextBox2中的答案是126.91并且舍入到最接近的整数,但它不会舍入数字。在TextBox2中显示126。我做错了什么,忘记了什么?



I have a code that should round to the nearest whole number and when I test it the number is not rounding. When I enter 1523 in TextBox1 that number is divided into 12 and the answer is 126.91 in TextBox2 and Rounded to the nearest whole number but it doesn't Round the number. 126 in displayed in TextBox2. What did I do wrong and what did I forget?

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
            int j = 12;
            TextBox2.Text = Convert.ToString(i / j);

            int a = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBox4.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBox5.Text.Replace(",", ""));
            TextBox6.Text = Convert.ToString(a + b + c + d);

            int g = Convert.ToInt32(TextBox6.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBox7.Text.Replace(",", ""));
            TextBox8.Text = Convert.ToString(g + f);


            int o = Convert.ToInt32(TextBoxLY1.Text.Replace(",", ""));
            int p = Convert.ToInt32(TextBox8.Text.Replace(",", ""));
            TextBoxF40.Text = Convert.ToString(Math.Round((Math.Abs(p - o) * 100.0 / ((o)))));
            TextBoxF40.Text = Math.Round(Convert.ToDouble(TextBoxFTE40.Text), 2).ToString();

            RangeValidatorLY1.Validate();
            RangeValidatorLY2.Validate();
            TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();
            TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));
            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox6.Text = string.Format("{0:0,0}", double.Parse(TextBox6.Text));
            TextBox8.Text = string.Format("{0:0,0}", double.Parse(TextBox8.Text));
            TextBox9.Focus();
        }

推荐答案

这有几个问题。

首先不要转换它到一个字符串,直到你需要在TextBox中显示它。

然后,你把它四舍五入然后用零小数位格式化。

有更好的选择用于解析输入字符串。

Several things are wrong with this.
First don't convert it to a string until you need to display it in the TextBox.
Then, you round it to 2 places and then format it with zero decimal places.
There are better choices for parsing the input string.
using System.Globalization;

int i;
if (!int.TryParse(TextBox1.Text, NumberStyles.Integer | NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out i))
{
  // parse failed, handle this as you think best
}
int j = 12;  // or declare this as double then the cast on the next line is unnecessary

double scaled = (double)i / j;

// this rounds to 2 decimal places!!!
scaled = Math.Round(scaled, 2);
// you probably want
scaled = Math.Round(scaled);

TextBox2.Text = string.Format("{0:0,0}", scaled);


尝试

Try
int i = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
TextBox2.Text = Convert.ToString(i / 12.0);





您应该阅读有关整数除法的C#文档。





You should read C# documentation about division with integers.

TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();
TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));



你看起来很复杂。

广泛的C#文档/教程阅读不会有害。


You look pretty complicated in your head.
Extensive C# documentation/tutorials reading will not harm.


这篇关于尝试舍入到最接近的整数,但舍入不起作用。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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