Working with Numbers
Learn about common data types for working with numbers in .NET.
One of the most common types of data is numbers. The most common types in .NET for working with numbers are shown in the following table:
Namespace | Example Type(s) | Description |
|
| Integers; that is, zero, and positive and negative whole numbers |
|
| Cardinals; that is, zero, and positive whole numbers, aka unsigned hence the U |
|
| Reals; that is, floating-point numbers |
|
| Accurate reals; that is, for use in science, engineering, or financial scenarios |
|
| Arbitrarily large integers, complex numbers, and quaternion numbers |
.NET has supported the 32-bit float
and 64-bit double
types since .NET Framework 1.0. The IEEE 754 specification also defines a 16-bit floating-point standard. Because machine learning and other algorithms benefit from smaller, lower-precision number types, Microsoft introduced the System.Half
type with .NET 5 and later. Currently, the C# language does not provide a half
alias, so you must use the .NET type System.Half
. This might change in the future.
Working with big integers
The largest whole number that can be stored in .NET types that have a C# alias is about eighteen and a half quintillion, stored in an unsigned ulong integer. But what if we need to store numbers larger than that?
Let’s explore numerics:
Step 1: Use your preferred code editor to create a new project, as defined in the following list:
Project template:
Console App
or
console
Project file and folder:
WorkingWithNumbers
Workspace/solution file and folder:
Chapter08
Step 2: In the project file, add an element to statically and globally import the ...