Numeric formats are pattern in which data (numeric) can be expressed. For example; limiting the float number to two number precision.

number format in c#, c# number format, c# decimal format, c# string format decimal, c# int to hex, c# currency format, c# string format leading zeros, c# format number with commas, c#,


The aim of this article is to introduce you to C# Numeric Formats. You'll grasp the various numeric formatting options available in C#. You can apply custom formatting to the numeric data if the traditional formatting fails to meet your need.

Consider the following table to have an understanding of the available formatting types:

Format Character

Type

Description

C or c

Currency

Used for currency formatting. The flag would by default prefix the local cultural mark (for US English, a dollar sign [$]).

D or d

Decimal

Used for decimal numbers formatting. This flag will also determine how many digits can be used to pad the value.

E or e

Exponential

It's a symbol for exponential notation. The case of the exponential constant determines whether it is uppercase (E) or lowercase (e).

F or f

Fixed-point

Fixed-point formatting is done in this. This flag will also determine how many digits can be used to pad the value.

G or g

General

Is an abbreviation for "general." This character can be used to change the type of a number from fixed to exponential.

N or n

Number

This symbol is used for basic numerical formatting with commas.

X or x

Hexadecimal

Hexadecimal formatting is done using this. The hex format can also include uppercase characters if you choose an uppercase X.

 String interpolation is used in the following example to create a string. Objects and expressions can be used as part of the string interpolation operation by using it. $ sign is used with the placeholder {…} within the string to output the value assigned to a variable at the position of the placeholder.

int number =34;

Console.WriteLine($"Number is {number}");

 

Output:

Number is 34

 

Consider the following examples for each of its type to have a better understanding of formatting the numbers.

Currency:

int number =345;

Console.WriteLine($"Number is {number:c}");

Console.WriteLine($"Number is {number:c0}");

Console.WriteLine($"Number is {number:c4}");

 

Output:

Number is $345.00

Number is $345

Number is $345.0000

 

By default, using “c” will show two decimal places. If you want to omit the zeros at the end, use “c0”.

Decimal:

int number =345;

Console.WriteLine($"Number is {number:d}");

Console.WriteLine($"Number is {number:d7}");

Console.WriteLine($"Number is {number:D4}");

 

Output:

Number is 345

Number is 0000345

Number is 0345

 Using “d” to format, the number is adding additional zeros at the start of the number to make it seven and four digits respectively.

Exponential:

int number =345;

Console.WriteLine($"Number is {number:E}");

Console.WriteLine($"Number is {number:e}");

Console.WriteLine($"Number is {number:E7}");

Console.WriteLine($"Number is {number:e4}");

 

Output:

Number is 3.450000E+002

Number is 3.450000e+002

Number is 3.4500000E+002

Number is 3.4500e+002

“E or e” format add the decimal point and append zeros at the end to make it to the length of specified digit. Moreover, the digits of original number after decimal point makes the exponent of E or e.

Fixed-point:

int number =345;

Console.WriteLine($"Number is {number:f}");

Console.WriteLine($"Number is {number:f7}");

Console.WriteLine($"Number is {number:f4}");

 

Output:

Number is 345.00

Number is 345.0000000

Number is 345.0000

 With this type of formatting you can add specified number of zeros after the decimal point.

General:

int number =345;

Console.WriteLine($"Number is {number:g0}");

Console.WriteLine($"Number is {number:g2}");

Console.WriteLine($"Number is {number:G3}");

 

Output:

Number is 345

Number is 3.5e+02

Number is 345

 It can be use as an alternative for exponential type formatting but with limited scope (depending upon the length of the number).

Number:

int number = 3456789;

Console.WriteLine($"Number is {number:n}");

Console.WriteLine($"Number is {number:n2}");

Console.WriteLine($"Number is {number:N5}");

 

Output:

Number is 3,456,789.00

Number is 3,456,789.00

Number is 3,456,789.00000

 Using this formatting you can add commas and the digit after N or n define the number of zeros after the actual number.

Hexadecimal:

int number = 3456;

Console.WriteLine($"Number is {number:X}");

Console.WriteLine($"Number is {number:x2}");

Console.WriteLine($"Number is {number:X5}");

 

Output:

Number is D80

Number is d80

Number is 00D80

 Using this formatting, you can convert the number to a hexadecimal number. X or x defines the letters in a hex number to be capital or small respectively. Moreover, it also adds the number of zeros before the actual hex value to make the number of the specified length.

That’s all about the numeric formats in c#. If you found this article helpful share it with others. Let me know your queries and suggestions in the comment section below. Do subscribe to my site and channel as I don’t want you to miss any update.

Moreover, you can also contact me and share your queries on Instagram, Facebook or Twitter. I would love to provide a solution to your issues.

Stay tuned and strive to learn. Keep learning, keep growing!