English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Nummerische Typen(Number)

C#面向对象(OOP)

Numerische TypenNormalerweise können Zahlen in zwei Typen unterteilt werden: Ganzzahlentypen und Fließkommartypen.

Floating-point typesEine Zahl ist eine Ganzzahl ohne Dezimalpunkt. Sie kann negativ oder positiv sein.

Ein Zahl ist eine Zahl mit einem oder mehreren Dezimalpunkten. Sie kann negativ oder positiv sein.

C# basiert auf ihrer Größe im Speicher und ihrer Fähigkeit, Zahlen zu speichern, verschiedene DatenTypen für Ganzzahlentypen und Fließkommartypen zu enthalten.

Das Diagramm zeigt die numerischen Typen in C#.

Numerische Typen

Ganzzahlentypen

Ganzzahlentypen sind Ganzzahlen mit einem Dezimalpunkt. C# umfasst vier DatenTypen für Ganzzahlen: Byte, Short, Int und Long (byte, short, int, long).

byte255Der DatenTyp 'byte' speichert von 0 bis8Zahlen. Es belegt

Bit. Der Schlüsselwort 'byte' ist das Alias für die Struktur 'Byte'.-128to127Zwischen negative Zahlen. Das Schlüsselwort 'sbyte' ist das Alias für die Struktur 'SByte'.

byte b1 = 255;
byte b2 = -128;// uint ui -128" kann nicht in "Byte" umgewandelt werden
sbyte sb1 = -128; 
sbyte sb2 = 127; 
Console.WriteLine(Byte.MaxValue);//255
Console.WriteLine(Byte.MinValue);//0
Console.WriteLine(SByte.MaxValue);//127
Console.WriteLine(SByte.MinValue);//-128

short

Der DatenTyp 'short' ist signed Ganzzahlen. Er kann-32,0768to32,0767Zwischen Zahlen. Er belegt16bit Speicherplatz. Der Schlüsselwort 'short' ist in .NET 'Int16'.16alias. The positive number. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU, or lu, then its type is ulong. The uint keyword is an alias for the UInt

Der DatenTyp 'ushort' ist unsigned Ganzzahlen. Er kann nur Werte von 0 bis65535Zwischen positive Zahlen. Der Schlüsselwort 'ushort' ist in .NET 'UInt16'.16alias. The positive number. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU, or lu, then its type is ulong. The uint keyword is an alias for the UInt

short s1 = -32768;
short s2 = 32767;
short s3 = 35000;//uint ui 35"000" kann nicht in "short" umgewandelt werden
ushort us1 = 65535;
ushort us2 = -32000; //uint ui -32"000" kann nicht in "ushort" umgewandelt werden
Console.WriteLine(Int16.MaxValue);//32767
Console.WriteLine(Int16.MinValue);//-32768
Console.WriteLine(UInt16.MaxValue);//65535
Console.WriteLine(UInt16.MinValue);//0

int

Der DatenTyp 'int' ist32bit signed integer. It can store from-2,0147,0483,0648to2,0147,0483,0647Zahlen. Das Schlüsselwort 'int' ist in .NET 'Int32'.32alias. The positive number. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU, or lu, then its type is ulong. The uint keyword is an alias for the UInt

uint ist32bit unsignierte Ganzzahlen. Der Schlüsselwort 'uint' ist in .NET 'UInt32'.32Ein Alias für die Struktur. Es kann Werte von 0 bis4,0294,0967,0295Positive Zahlen. (Optional) Verwenden Sie nach der Zahl das Präfix U oder u, um es einer uint-Variablen zuzuweisen.

int i = -2147483648;
int j = 2147483647;
int k = 4294967295; //Kompilationsfehler: Der Typ 'uint' kann nicht implizit in 'int' umgewandelt werden.
uint ui1 = 4294967295;
uint ui2 =-1; //uint ui -1Compile-time error: constant value “
Console.WriteLine(Int32.MaxValue);//2147483647
Console.WriteLine(Int32.MinValue);//-2147483648
Console.WriteLine(UInt32.MaxValue);//4294967295
Console.WriteLine(UInt32.MinValue);//0

” cannot be converted to “uint”7.2Start, binary numbers are prefixed with 0b or 0B. The int data type is also used for hexadecimal and binary numbers. Hexadecimal numbers are prefixed with 0x or 0X. From C#

int hex = 0x2F;
int binary = 0b_0010_1111;
Console.WriteLine(hex);
Console.WriteLine(binary);

long

The long type is64bit signed integer. It can store from-9,0223,037236,0854,0775,0808to9,0223,037236,0854,0775,0807Use the numeric suffix l or L to assign it to a long type variable. The long keyword is an alias for the Int64alias. The positive number. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU, or lu, then its type is ulong. The uint keyword is an alias for the UInt

The ulong type stores numbers from 0 to18,0446,074473,0709,0551,0615,64alias. The positive number. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU, or lu, then its type is ulong. The uint keyword is an alias for the UInt

long l1 = -9223372036854775808;
long l2 = 9223372036854775807;
ulong ul1 = 18223372036854775808ul;
ulong ul2 = 18223372036854775808UL;
Console.WriteLine(Int64.MaxValue);//9223372036854775807
Console.WriteLine(Int64.MinValue);//-9223372036854775808
Console.WriteLine(UInt64.MaxValue);//18446744073709551615
Console.WriteLine(UInt64.MinValue);//0

Floating-point types

Floating-point numbers are positive or negative numbers with one or more decimal points. C# includes three floating-point number data types: float, double, and decimal (float, double, decimal).

float

The float data type can store from3.4ee038to3.4e + 038fraction. It occupies4bytes. The float keyword is an alias for the Single structure in .NET.

Use the text suffix f or F to make it a floating-point type.

float f1 = 123456.5F;
float f2 = 1.123456f;
Console.WriteLine(f1);//123456.5
Console.WriteLine(f2);//1.123456

double

The double data type can store from1.7eˆ308to1.7e + 308decimal. It occupies8bytes. The double keyword is an alias for the Double structure in .NET.

Use the text suffix d or D to make it a double-precision type.

double d1 = 12345678912345.5d;
double d2 = 1.123456789123456d;
Console.WriteLine(d1);//12345678912345.5
Console.WriteLine(d2);//1.123456789123456

decimal

The decimal data type can store from ±1.0 x 10-28to ±7.9228 x 1028decimal. It occupies16bytes. Decimal is an alias keyword for the Decimal structure in .NET.

The decimal type has higher precision and a smaller range than floating-point and double-precision types, and is therefore suitable for financial and monetary calculations.

Use the m or M suffix with text to make it a decimal type.

decimal d1 = 123456789123456789123456789.5m;
decimal d2 = 1.1234567891345679123456789123m;
Console.WriteLine(d1);
Console.WriteLine(d2);

Wissenschaftliche Notation

Verwenden Sie e oder E, um darzustellen10Die Potenz, als Exponententeil der wissenschaftlichen Notation, wird als Fließkomma, Doubleprecision oder Dezimalzahl verwendet.

double d = 0.12e2;
Console.WriteLine(d);  // 12;
float f = 123.45e-2f;
Console.WriteLine(f);  // 1.2345
decimal m = 1.2e6m;
Console.WriteLine(m);// 1200000