首页 > 其他分享 >.NET 数值类型

.NET 数值类型

时间:2022-12-28 11:56:39浏览次数:41  
标签:Console ---------------------------------- MaxValue 数值 Int128 WriteLine 类型 NET M

在C#中,有int16,用short来定义;有int32,用int定义;用int64,用long来定义。在.NET7中,添加了int128,和unint128,位数更大的整型。

接下来,我们在.NET Console 测试一下结果:

var int16 = short.MaxValue;
Console.WriteLine($"Int16(short):{int16}");
var int32 = int.MaxValue;
Console.WriteLine($"Int32(int):{int32}");
var int64 = long.MaxValue;
Console.WriteLine($"Int64(long):{int64}");
var int128 = Int128.MaxValue;
Console.WriteLine($"Int128(nint):{int128}");

BigInteger bi = Int128.MaxValue + 1;
Console.WriteLine($"Int128(BigInteger):{bi}");

BigInteger bi25 = Int128.MaxValue;
Console.WriteLine($"Int128(BigInteger25):{bi * bi }");

 输出结果为:

 

 

 BigInteger 类型是怎么Big。

接下来看下所以数字类型的测试以及所占的字符:

 1 using System.Runtime.InteropServices;
 2 
 3 Console.WriteLine($"short max:{short.MaxValue}");
 4 Console.WriteLine($"short min:{short.MinValue}");
 5 short s = 123;
 6 Console.WriteLine($"short值:{s},占{Marshal.SizeOf(s)}字节");
 7 Console.WriteLine("----------------------------------");
 8 
 9 Console.WriteLine($"int max:{int.MaxValue}");
10 Console.WriteLine($"int min:{int.MinValue}");
11 int i = 123;
12 Console.WriteLine($"int值:{i},占{Marshal.SizeOf(i)}字节");
13 Console.WriteLine("----------------------------------");
14 
15 Console.WriteLine($"nint max:{nint.MaxValue}");
16 Console.WriteLine($"nint min:{nint.MinValue}");
17 nint n = 123;
18 Console.WriteLine($"nint值:{n},占{Marshal.SizeOf(n)}字节");
19 Console.WriteLine("----------------------------------");
20 
21 Console.WriteLine($"long max:{long.MaxValue}");
22 Console.WriteLine($"long min:{long.MinValue}");
23 long l = 123;
24 Console.WriteLine($"long值:{l},占{Marshal.SizeOf(l)}字节");
25 Console.WriteLine("----------------------------------");
26 
27 Console.WriteLine($"Int128 max:{Int128.MaxValue}");
28 Console.WriteLine($"Int128 min:{Int128.MinValue}");
29 Int128 i128 = 123;
30 Console.WriteLine($"Int128值:{i128},占{Marshal.SizeOf(i128)}字节");
31 Console.WriteLine("----------------------------------");
32 
33 Console.WriteLine($"Half max:{Half.MaxValue}");
34 Console.WriteLine($"Half min:{Half.MinValue}");
35 Half h = (Half)43210.123456789;
36 Console.WriteLine($"Half值:{h},占{Marshal.SizeOf(h)}字节");
37 Console.WriteLine("----------------------------------");
38 
39 Console.WriteLine($"float max:{float.MaxValue}");
40 Console.WriteLine($"float min:{float.MinValue}");
41 float f = 0.123456789f;
42 Console.WriteLine($"float值:{f},占{Marshal.SizeOf(f)}字节");
43 Console.WriteLine("----------------------------------");
44 
45 Console.WriteLine($"double max:{double.MaxValue}");
46 Console.WriteLine($"double min:{double.MinValue}");
47 double d = 0.123456789012345678d;
48 Console.WriteLine($"double值:{d},占{Marshal.SizeOf(d)}字节");
49 
50 Console.WriteLine("----------------------------------");
51 
52 Console.WriteLine($"decimal max:{decimal.MaxValue}");
53 Console.WriteLine($"decimal min:{decimal.MinValue}");
54 var m = 0.12345678901234567890123456m;
55 Console.WriteLine($"decimal值:{m},占{Marshal.SizeOf(m)}字节");
56 Console.WriteLine("----------------------------------");

输出结果:

 

标签:Console,----------------------------------,MaxValue,数值,Int128,WriteLine,类型,NET,M
From: https://www.cnblogs.com/Early/p/17009814.html

相关文章