.NET HELP

C# Numeric Types (How It Works For Developers)

Updated June 20, 2023
Share:

Understanding C# numeric types is foundational for anyone who wants to master C#. They form the basis of numerous operations, from simple mathematical calculations to complex algorithms. Let's delve into these fundamental building blocks of C#

What are Numeric Types in C#

In C#, numeric types are a set of data types used to store numbers. These can be divided into two main categories: integer data types and floating-point types. Each category has its own unique characteristics and use cases.

Integer Data Types

Integer data types are utilized to store whole numbers, which means numbers without a decimal point. They come in various sizes and can be either signed, capable of holding both positive and negative numbers, or unsigned, capable of holding only positive numbers.

Here's a breakdown of the integer types in C#:

byte

The byte is the smallest integer type. It's an unsigned type, with a default value of 0, capable of storing the same value from 0 to 255.

sbyte

The sbyte is a signed counterpart of the byte. It can store values from -128 to 127, with a default value of 0.

short

A short is a 16-bit signed integer. It has a larger range than the byte and sbyte, from -32768 to 32767, with a default value of 0.

ushort

ushort is the unsigned counterpart of the short. It can hold values from 0 to 65535. Its default value is also 0.

int

An int is a 32-bit signed integer type, with a range from -2,147,483,648 to 2,147,483,647. The default value of an integer variable of type int is 0.

uint

The uint, short for "unsigned integer," can hold values from 0 to 4,294,967,295. Its default value is 0.

long

Long is a 64-bit signed integer type, capable of storing values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The default value of a long is 0L.

ulong

The ulong, or unsigned long, can store values from 0 to 18,446,744,073,709,551,615. Its default value is 0UL.

Floating-Point Types

Floating-point types are used to store numbers with a decimal point. They provide a way to represent a much wider range of values than integer types, but with a trade-off in precision.

Here are the floating-point types in C#:

float

A float is a 32-bit floating-point type. It can represent values from approximately 1.5 x 10^-45 to 3.4 x 10^38, with a precision of 7 digits. The default value of a floating-point variable of type float is 0.0f.

double

The double type is a 64-bit floating-point type. It can represent values from approximately 5.0 x 10^-324 to 1.7 x 10^308, with a precision of 15-16 digits. The default value of a double is 0.0d.

decimal

The decimal type is a 128-bit data type. It's often used when dealing with financial calculations that require high precision. It can represent values from approximately 1.0 x 10^-28 to 7.9 x 10^28, with a precision of 28-29 digits. The default value of a decimal type is 0.0m.

Understanding Native Sized Integer Types

Native sized integer types are special types in C# that have a size that varies depending on the platform the code is running on. These are designed to provide the most efficient use of memory for storing integer values.

IntPtr

IntPtr is a signed integer type that has the same width as a pointer. This means it's 32-bit on 32-bit platforms and 64-bit on 64-bit platforms. This is useful when dealing with pointer or memory-related tasks, and the default value of an IntPtr is 0.

UIntPtr

UIntPtr, the unsigned counterpart of IntPtr, also has the same size as a pointer. It provides the same value range as IntPtr on the same platform, but only for non-negative values. The default value of an UIntPtr is also 0.

Converting Between Numeric Types

Converting between different numeric types in C# is also a crucial part of understanding how to use them effectively. In many cases, you may have a value of one type and need to use it as another type. C#

Implicit conversions happen automatically when the value to be converted will fit into the new data type without losing any information. For example, you can implicitly convert an int to a long, as a long can store the same value as an int.

Explicit conversions, also known as casting, are required when there's a risk of data loss. For example, converting a long to an int may result in data loss if the long's value is larger than what an int can hold.

To explicitly cast a value from one type to another, you can use the casting operator ():

long myLong = 5000L;
int myInt = (int)myLong; // This is an explicit cast.
long myLong = 5000L;
int myInt = (int)myLong; // This is an explicit cast.
Dim myLong As Long = 5000L
Dim myInt As Integer = CInt(myLong) ' This is an explicit cast.
VB   C#

Be careful when casting, as it can lead to unexpected results if the value is outside the range of the target type.

Applying C#

IronPDF is a powerful library for creating and manipulating PDF documents in C# using HTML. Numeric types in C#

IronPDF’s HTML to PDF function is a highlight, maintaining your layouts and styles. It allows for PDF creation from web content, perfect for reports, invoices, and documentation. HTML files, URLs, and HTML strings can be converted to PDFs easily.

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()

		' 1. Convert HTML String to PDF
		Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
		Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
		pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")

		' 2. Convert HTML File to PDF
		Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
		Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
		pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")

		' 3. Convert URL to PDF
		Dim url = "http://ironpdf.com" ' Specify the URL
		Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
		pdfFromUrl.SaveAs("URLToPDF.pdf")
	End Sub
End Class
VB   C#

Positioning and Sizing Elements

Here is the example of using numeric types with IronPDF is when positioning and sizing elements on a page. Coordinates in IronPDF are defined using float data type.


using IronPdf;
var htmlToPdf = new ChromePdfRenderer();
htmlToPdf.RenderingOptions.MarginTop= 50; // Set top margin. Value is in points.
htmlToPdf.RenderingOptions.MarginBottom= 50; // Set bottom margin. Value is in points.
var document = htmlToPdf.RenderHtmlAsPdf("<h1>Numberic Types in C# with IronPDF</h1>");
document.SaveAs("C:\\numericTypes.pdf");

using IronPdf;
var htmlToPdf = new ChromePdfRenderer();
htmlToPdf.RenderingOptions.MarginTop= 50; // Set top margin. Value is in points.
htmlToPdf.RenderingOptions.MarginBottom= 50; // Set bottom margin. Value is in points.
var document = htmlToPdf.RenderHtmlAsPdf("<h1>Numberic Types in C# with IronPDF</h1>");
document.SaveAs("C:\\numericTypes.pdf");
Imports IronPdf
Private htmlToPdf = New ChromePdfRenderer()
htmlToPdf.RenderingOptions.MarginTop= 50 ' Set top margin. Value is in points.
htmlToPdf.RenderingOptions.MarginBottom= 50 ' Set bottom margin. Value is in points.
Dim document = htmlToPdf.RenderHtmlAsPdf("<h1>Numberic Types in C# with IronPDF</h1>")
document.SaveAs("C:\numericTypes.pdf")
VB   C#

Output PDF File

C# Numeric Types (How It Works For Developers) Figure 1

Conclusion

Understanding C# numeric types is crucial for effective programming, as they play a significant role in various operations, including basic calculations and PDF generation using IronPDF. They serve as the building blocks for data manipulation, algorithm design, and the creation of high-quality PDF documents.

As you explore more of C# and its applications, like IronPDF, the importance of numeric types will only become more apparent. IronPDF is a practical tool for any .NET developer's toolkit. If you're interested, IronPDF offers a free trial for you to test its capabilities. Once you're ready to commit, licenses start from $749.

< PREVIOUS
Blazor vs MVC (How It Works For Developers)
NEXT >
C# Types (How It Works For Developers)

Ready to get started? Version: 2024.7 just released

Free NuGet Download Total downloads: 9,848,088 View Licenses >
123