gpt4 book ai didi

c# - 算术运算 : Int to Double

转载 作者:行者123 更新时间:2023-11-30 13:22:25 25 4
gpt4 key购买 nike

我在我的 Windows 应用程序中使用了 2 个文本框和一个按钮。

textBox1 用于用户输入

textBox2 显示算术运算的结果。

当用户点击一个按钮时,应该执行以下操作:

1000 * textbox1.text / 60

为此我使用了

private void button1_Click(object sender, EventArgs e)
{
int Result = 1000 * Convert.ToInt32(textBox1.Text) / 60;

textBox2.Text = Result.ToString();
}

然而,我得到的值是一个整数,即使我期望它是一个 double 。

我该如何解决这个问题?

最佳答案

规则是:

When one integer is divided by another, the arithmetic is performed as integer arithmetic. If you want it to be performed as float, double or decimal arithmetic, you need to cast one of the values appropriately.

所以所有这些都会起作用:

double Result = 1000 * Convert.ToDouble(textBox1.Text) / 60;
double Result = 1000 * Convert.ToInt32(textBox1.Text) / (double)60;
double Result = 1000 * Convert.ToInt32(textBox1.Text) / 60.0;

来自 MSDN关于整数除法:

The result is the integer quotient of number1 divided by number2. The integer quotient discards any remainder and retains only the integer portion.

关于c# - 算术运算 : Int to Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com