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



posted @   echo-efun  阅读(1875)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示