2: Data Types in C#.NET

Drawer PDF-417 2d barcode in C#.NET 2: Data Types
2: Data Types
Making PDF417 In Visual C#
Using Barcode generation for .NET Control to generate, create PDF 417 image in .NET applications.
TABLE 22: Floating-Point Types
Barcode Printer In Visual C#
Using Barcode generator for VS .NET Control to generate, create bar code image in .NET framework applications.
Type
PDF 417 Maker In .NET
Using Barcode encoder for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
float
PDF417 Generation In .NET Framework
Using Barcode generation for .NET framework Control to generate, create PDF417 image in Visual Studio .NET applications.
Size 32 bits
PDF-417 2d Barcode Printer In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create PDF 417 image in VS .NET applications.
Range (Inclusive) 15 10 45 to 34 1038 50 10 324 to 17 10308
Create Bar Code In Visual C#
Using Barcode creation for .NET Control to generate, create barcode image in .NET framework applications.
BCL Name
Bar Code Maker In Visual C#
Using Barcode creator for .NET framework Control to generate, create bar code image in .NET applications.
SystemSingle
Print USS Code 39 In Visual C#
Using Barcode printer for Visual Studio .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
Significant Digits 7
Code 128 Maker In C#.NET
Using Barcode maker for .NET Control to generate, create Code-128 image in VS .NET applications.
double
Data Matrix Drawer In C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
64 bits
Encoding GTIN - 12 In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Universal Product Code version A image in ASP.NET applications.
SystemDouble
Bar Code Drawer In VS .NET
Using Barcode drawer for .NET framework Control to generate, create bar code image in .NET applications.
15 16
Print Code 39 In Java
Using Barcode creator for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.
Decimal Type C# contains a numeric type with 128-bit precision (see Table 23) This is suitable for large and precise calculations, frequently financial calculations
Draw Data Matrix ECC200 In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create ECC200 image in VS .NET applications.
TABLE 23: decimal Type
Data Matrix 2d Barcode Maker In Java
Using Barcode drawer for Java Control to generate, create ECC200 image in Java applications.
Type
Making Code-39 In VS .NET
Using Barcode encoder for VS .NET Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
decimal
Generate EAN13 In .NET
Using Barcode generator for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
Size 128 bits
Drawing EAN / UCC - 14 In .NET
Using Barcode maker for Visual Studio .NET Control to generate, create GTIN - 128 image in .NET applications.
Range (Inclusive) 10 10 28 to approximately 79 1028
Barcode Drawer In VB.NET
Using Barcode creation for .NET framework Control to generate, create barcode image in .NET framework applications.
BCL Name
Code 3 Of 9 Creator In .NET
Using Barcode creation for ASP.NET Control to generate, create Code 39 Full ASCII image in ASP.NET applications.
SystemDecimal
Significant Digits 28 29
Unlike floating-point numbers, the decimal type maintains exact precision for all denary numbers within its range With the decimal type, therefore, a value of 01 is exactly 01 However, although the decimal type has greater precision than the floating-point types, it has a smaller range Thus, conversions from floating-point types to the decimal type may result in overflow errors Also, calculations with decimal are slightly slower
ADVANCED TOPIC Floating-Point Types and Decimals Dissected Unless they are out of range, decimal numbers represent denary numbers exactly In contrast, the floating-point representation of many denary numbers introduces a rounding error The difference between the decimal type and the C# floating-point types is that the exponent of a decimal type is a denary and the exponent of floating-point types is binary
Fundamental Numeric Types
The exponent of a decimal is N * 10k where
N is a positive integer represented by 96 bits k is given by -28 <= k <= 0
In contrast, a float is any number N * 2k where
N is a positive integer represented by a fixed number of bits (24 for float and 53 for double) k is any integer ranging from -149 to +104 for float and -1075 to +970
for double
Literal Values A literal value is a representation of a constant value within source code For example, if you want to have SystemConsoleWriteLine() print out the integer value 42 and the double value 1618034 (Phi), you could use the code shown in Listing 21
Listing 21: Specifying Literal Values
SystemConsoleWriteLine(42); SystemConsoleWriteLine(1618034);
Output 21 shows the results of Listing 21
OUTPUT 21:
42 1618034
BEGINNER TOPIC Use Caution When Hardcoding Values The practice of placing a value directly into source code is called hardcoding, because changing the values means recompiling the code Developers must carefully consider the choice between hardcoding values within their code and retrieving them from an external source, such as a configuration file, so that the values are modifiable without recompiling
2: Data Types
By default, when you specify a literal number with a decimal point, the compiler interprets it as a double type Conversely, a literal value with no decimal point generally defaults to an int, assuming the value is not too large to be stored in an integer If the value is too large, the compiler will interpret it as a long Furthermore, the C# compiler allows assignment to a numeric type other than an int, assuming the literal value is appropriate for the target data type short s = 42 and byte b = 77 are allowed, for example However, this is appropriate only for literal values; b = s is not appropriate without additional syntax, as discussed in the section Conversions between Data Types, later in this chapter As previously discussed in the section Fundamental Numeric Types, there are many different numeric types in C# In Listing 22, a literal value is placed within C# code Since numbers with a decimal point will default to the double data type, the output, shown in Output 22, is 161803398874989 (the last digit, 5, is missing), corresponding to the expected accuracy of a double
Listing 22: Specifying a Literal double
SystemConsoleWriteLine(1618033988749895);
OUTPUT 22:
161803398874989
To view the intended number with its full accuracy, you must declare explicitly the literal value as a decimal type by appending an m (or M) (see Listing 23 and Output 23)
Listing 23: Specifying a Literal decimal
SystemConsoleWriteLine(1618033988749895m);
OUTPUT 23:
1618033988749895
Fundamental Numeric Types
Now the output of Listing 23 is as expected: 1618033988749895 Note that d is for double The m used to identify a decimal corresponds to its frequent use in monetary calculations You can also add a suffix to a value to explicitly declare a literal as float or double by using the f and d suffixes, respectively For integer data types, the suffixes are u, l, lu, and ul You can determine the type of an integer literal as follows