using System;namespace nmy { public class test { int i; int j; public test() { i = 102; j = 15; } public static void Main(string[] args) { test a = new test(); double d = a.i / a.j; decimal f = a.i / a.j; decimal k = System.Math.Ceiling(f); double l = System.Math.Ceiling(d); double m = System.Math.Ceiling(6.8); int s = Convert.ToInt32(k); Console.WriteLine("i:{0}", a.i); Console.WriteLine("j:{0}", a.j); Console.WriteLine("double:{0}", l); Console.WriteLine("double-->:{0}", m); Console.WriteLine("decimal:{0}", k); Console.WriteLine("int:{0}", s); Console.ReadLine(); } }}自己编译一下 看看, 只能说 hhh=102/15的时候 把数值改为6.0了 并不能说hhh的值是6.8
double hhh;
hhh = 102 / 15;那么 hhh的值就是 6.0 不信 你去编译一下看看,6.0 如果转换之后再用那个方法 肯定是6了如果你的结果是6.8的话 肯定是7了 自己把根据找踏实了再问
{
public class test
{
int i;
int j;
public test()
{
i = 102;
j = 15;
} public static void Main(string[] args)
{
test a = new test(); double d = a.i / a.j;
decimal f = a.i / a.j;
decimal k = System.Math.Ceiling(f);
double l = System.Math.Ceiling(d);
double m = System.Math.Ceiling(6.8);
int s = Convert.ToInt32(k);
Console.WriteLine("i:{0}", a.i);
Console.WriteLine("j:{0}", a.j);
Console.WriteLine("double:{0}", l);
Console.WriteLine("double-->:{0}", m);
Console.WriteLine("decimal:{0}", k);
Console.WriteLine("int:{0}", s);
Console.ReadLine();
}
}}自己编译一下 看看, 只能说 hhh=102/15的时候 把数值改为6.0了 并不能说hhh的值是6.8
---------------------------------------
一个看似原因在a处 结果发现在b处,最后总结可能在c处。现在问题是,net 结果得和除数被除数的数据类型一样吗?
造成102/15=6.0的原因 在哪?是int /int =int 得6 然后因为我将变量声明为了double 所以 变为6.0
还是 int/int =double 自动将结果小数省略所以得 6.0
还是有别的原因?
double hhh;
hhh = 102 / 15;整型运算 结果为6 赋值给hhh
ppp = Convert.ToInt32(Math.Ceiling(hhh)); 不是6怪了
初学者的经典错误
double hhh;
hhh = 102 / 15.0;
ppp = Convert.ToInt32(Math.Ceiling(hhh));