Reader Level:
Article

Standard Numeric Format String In C#

By Richa Garg on Oct 22, 2012
In this article I will explain standard numeric format specifiers and how to use them.

Introduction

The standard numeric Format Specifiers can be used to convert numbers to strings in various formats. There are various formats in which a number can be converted; the list of format types is:

Number ( "N") Format Specifier
Decimal ("D") Format Specifier
Exponential ("E") Format Specifier
Percent ("P") Format Specifier
Fixed-Point ("F") Format Specifier

Number Format Specifier

This Format Specifier can be used to convert a number into a string of the form "-d,ddd,ddd.ddd....." where d represents any number between 0 to 9. The negative sign represents a negative number and "." denotes the decimal point in the number and "," denotes the group separator.

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double dblValue = -12445.6789;

            Console.WriteLine("The double value is as : " + dblValue);

            Console.WriteLine("The Number Formatting is as :"+ dblValue.ToString("N", CultureInfo.InvariantCulture));

        }

    }

}

Output

Number-format-specifier-in-csharp.jpg

Decimal Format Specifier

This decimal Format Specifier string can be used to convert a number into the decimal format string as in:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            int value= 12345;

            Console.WriteLine("The value is :" + value);

            Console.WriteLine("The decimal format string is :"+ value.ToString("D"));           

            Console.WriteLine("The decimal format string with 8 number digits is as :"+ value.ToString("D8"));         

        }

    }

}

Output

Decimal-format-specifier-in-asharp.jpg

Exponential Format Specifier

The exponential ("E") Format Specifier converts a number to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative. See:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double value = 12345.6789;

            Console.WriteLine("The value is as :" + value);

            Console.WriteLine("the exponential value is as :" + value.ToString("E", CultureInfo.InvariantCulture));

        }

    }

}

Output

Exponential-format-specifier-in-csharp.jpg

Percent Format Specifier

The percent ("P") Format Specifier multiplies a number by 100 and converts it to a string that represents a percentage, as in:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double number = .2468013;

            Console.WriteLine("The number is as :" + number);

            Console.WriteLine("The percent value is as : "+ number.ToString("P", CultureInfo.InvariantCulture));

        }

    }

}


Output

percent-format-specifier-in-csharp.jpg

Fixed Point Format Specifier

The fixed-point ("F) Format Specifier converts a number to a string of the form "-ddd.ddd…" where each "d" indicates a digit (0-9). See:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            int Number = 17843;

            Console.WriteLine("The number is as :" + Number);

            Console.WriteLine("The fixed point number is as :"+ Number.ToString("F",CultureInfo.InvariantCulture));

        }

    }

}

Output

fixed-point-format-specifier-in-csharp.jpg

Summary

In this article I explained all the standard Format Specifiers using C#.

 

Richa Garg

I am a Junior Software Developer In MCN Solutions Pvt. ltd. and very much interested in .net technology : Asp.net, C#, WPF,  SQL Server, Windows Store apps, HTML, DHTML, CSS etc......

COMMENT USING