C# 为什么这些除法方程的结果为零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1205490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do these division equations result in zero?
提问by Edward Tanguay
The result of all of the division equations in the below for loop is 0. How can I get it to give me a decimal e.g.:
下面 for 循环中所有除法方程的结果都是 0。我怎样才能得到它给我一个小数,例如:
297 / 315 = 0.30793650793650793650793650793651
Code:
代码:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
decimal result = i / 100;
long result2 = i / 100;
double result3 = i / 100;
float result4 = i / 100;
Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})", i, 100, i / 100, result, result2, result3, result4);
}
Console.ReadLine();
}
}
}
Answer:
回答:
Thanks Jon and everyone, this is what I wanted to do:
谢谢乔恩和大家,这就是我想做的:
using System;
namespace TestDivide
{
class Program
{
static void Main(string[] args)
{
int maximum = 300;
for (int i = 0; i <= maximum; i++)
{
float percentage = (i / (float)maximum) * 100f;
Console.WriteLine("on #{0}, {1:#}% finished.", i, percentage);
}
Console.ReadLine();
}
}
}
采纳答案by Jon Skeet
You're using int/int, which does everything in integer arithmetic even if you're assigning to a decimal/double/float variable.
您正在使用 int/int,即使您分配给小数/双/浮点变量,它也可以执行整数算术中的所有操作。
Force one of the operands to be of the type you want to use for the arithmetic.
强制操作数之一为您要用于算术的类型。
for (int i = 0; i <= 100; i++)
{
decimal result = i / 100m;
long result2 = i / 100;
double result3 = i / 100d;
float result4 = i / 100f;
Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})",
i, 100, i / 100d, result, result2, result3, result4);
}
Results:
结果:
0/100=0 (0,0,0, 0)
1/100=0.01 (0.01,0,0.01, 0.01)
2/100=0.02 (0.02,0,0.02, 0.02)
3/100=0.03 (0.03,0,0.03, 0.03)
4/100=0.04 (0.04,0,0.04, 0.04)
5/100=0.05 (0.05,0,0.05, 0.05)
(etc)
(等等)
Note that that isn'tshowing the exact value represented by the float or the double - you can'trepresent 0.01 exactly as a float or double, for example. The string formatting is effectively rounding the result. See my article on .NET floating binary pointfor more information as well as a class which will let you see the exactvalue of a double.
请注意,这并未显示浮点数或双精度数表示的确切值 - 例如,您不能将 0.01 精确地表示为浮点数或双精度数。字符串格式有效地四舍五入结果。请参阅我关于 .NET 浮点二进制点的文章以获取更多信息以及一个可以让您查看双精度值的确切值的类。
I haven't bothered using 100L for result2
because the result would always be the same.
我没有为使用 100 升而烦恼,result2
因为结果总是一样的。
回答by Matthieu
Because iis a intvalue and you divide by an integer so the result is an integer ! and so you need to divide by 100.0to have an implicit cast in float or specify 100f or 100d
因为i是一个int值,你除以一个整数所以结果是一个整数!所以你需要除以100.0以在 float 中进行隐式转换或指定 100f 或 100d
回答by Lloyd
Try
尝试
i / 100.0
回答by dfa
because i
is an int: i / 100
performs integer division, then the result, that is always 0, is casted to the target type. You need to specify at least one non-int literal in your expression:
因为i
是int:i / 100
执行整数除法,然后将始终为 0 的结果强制转换为目标类型。您需要在表达式中至少指定一个非整型文字:
i / 100.0
回答by Scoregraphic
Because i is an integer and 100 is an integer...so you have an integer division
因为 i 是一个整数而 100 是一个整数...所以你有一个整数除法
Try (decimal)i / 100.0 instead
尝试(十进制)i / 100.0 代替
回答by lotsoffreetime
No matter where you store it, an integer divided by an integer will always be an integer.
无论您将它存储在何处,整数除以整数将始终为整数。
回答by Ahmed Said
this is integer division whatever the type of variable you storing in, so int / int = int
无论您存储的变量类型如何,这都是整数除法,因此 int / int = int
回答by Steve Gilham
double result3 = ((double)i) / 100;
回答by Tormod
You need to force a floating point operation "double / double" instead of an "int / int"
您需要强制执行浮点运算“double / double”而不是“int / int”
double result = (double)297 / (double)315 ;
回答by Yohan Dahmani
In my case I had only vars and no int
就我而言,我只有 vars 而没有 int
float div = (var1 - var2) / float.Parse(var1.ToString());