Numeric formats are pattern in which data (numeric) can be expressed. For example; limiting the float number to two number precision.
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. |
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:
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 |
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 |
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 |
Number:
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 |
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 |
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!
0 Comments