float类型,没问题:
float x = 0.7f;
Console.WriteLine("x=" + x.ToString());//x = 0.7
int a = (int)x;
Console.WriteLine("a=" + a.ToString());//a = 0
float x10 = x * 10;
Console.WriteLine("x10=" + x10.ToString());//x10 = 7.0
int a10 = (int)x10;
Console.WriteLine("a10=" + a10.ToString());//a10 = 7
double类型,没问题:
double x = 0.7;
Console.WriteLine("x=" + x.ToString());//x = 0.7
int a = (int)x;
Console.WriteLine("a=" + a.ToString());//a = 0
double x10 = x * 10;
Console.WriteLine("x10=" + x10.ToString());//x10 = 7.0
int a10 = (int)x10;
Console.WriteLine("a10=" + a10.ToString());//a10 = 7
float转double类型,精度不对:
double x = 0.7f;//float转double
Console.WriteLine("x=" + x.ToString());//x = 0.699999988079071
int a = (int)x;
Console.WriteLine("a=" + a.ToString());//a = 0
double x10 = x * 10;
Console.WriteLine("x10=" + x10.ToString());//x10 = 6.99999988079071
int a10 = (int)x10;
Console.WriteLine("a10=" + a10.ToString());//a10 = 6
double和decimal:
bool b1 = (0.1 + 0.2 == 0.3);
Console.WriteLine(b1 ? "true" : "false");//false
bool b2 = (0.1m + 0.2m == 0.3m);
Console.WriteLine(b2 ? "true" : "false");//true