C# 中数字常见操作及其取值范围


1.浮点数

  用于表示数量级可能非常大或者非常小的非整数;

  float:单精度浮点数表示用于存储值的二进制位数为32位

  double:双精度浮点数相对于单精度浮点数而言,是其两倍;即表示用于存储值的二进制位数为64位

2.常见算数运算
  
   int c=7/4; //若值不为整数,商取整
  Console.WriteLine(c); //1
   double d = 7/4; //若值不为整数,商取整
  Console.WriteLine(d); //1
  d = 7/(4*1.0);
  Console.WriteLine(d);  //1.75

 

1
1
1.75

  
    int c = 7 %4;
  Console.WriteLine(c); //3,取余
  c =4 % 7;
  Console.WriteLine(c); //4,取余
  int e = 11%7; //11%7
  Console.WriteLine(e); //4
  e = 7 % 11;
  Console.WriteLine(e); //7  

 

3
4
4
7
2.常见数值类型取值范围
  
    int maxI = int.MaxValue;
  int minI = int.MinValue;
  Console.WriteLine($"The range of integers is {minI} to {maxI}");

   The range of integers is -2147483648 to 2147483647

double maxD = double.MaxValue;
double minD = double.MinValue;
Console.WriteLine($"The range of double is {minD} to {maxD}");

 The range of double is -1.79769313486232E+308 to 1.79769313486232E+308

  其中E+308表示10的308次方

decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");
The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335
 
int what = maxI + 1;
Console.WriteLine($"int.MaxValue ={int.MaxValue}  int.MaxValue+1= {what}"); //最大整数加1刚好取反得到最小负数
what = maxI + 2;
Console.WriteLine($"int.MaxValue ={int.MaxValue}  int.MaxValue+2= {what}");

 

int.MaxValue =2147483647  int.MaxValue+1= -2147483648
int.MaxValue =2147483647  int.MaxValue+2= -2147483647
 
double和decimal类型比较
double a1 = 1.0;
double b1 = 3.0;
Console.WriteLine(a1 / b1);
0.333333333333333

decimal c1 = 1.0M;   //此处加M,类似float类型后面加f
decimal d1 = 3.0M;
Console.WriteLine(c1 / d1);
0.3333333333333333333333333333




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM