Introduction
In this article I will going to explain what is Fixed point format specifier and how to use it in C#, This format specifier string is used to convert a number to the string into the format "-ddd.ddd", where "d" denotes the number between 0 to 9, "-" denotes the negative sign (in other words the number is negative here) and "." denotes the decimal points. The result string generated by this formatting information is affected by the NumberFormatInfo class. The namespace that we use here is System.globalization. To work with this open Visual Studio 2010 and then click "File" -> "New" -> "Project...". A window is shown; in it select Console application under visual C# and give the name of your application that you want to give then click ok.
Apply a different Fixed-Point specifier in a positive integer number.
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int integerNumber;
integerNumber = 17843;
//by default after the decimal point the number digits are 2.
Console.WriteLine("The Fixed-point format specifier for an integer value is : " + integerNumber.ToString("F", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 3
Console.WriteLine("The Number with 3 digits after the decimal point : " + integerNumber.ToString("F3", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 4
Console.WriteLine("The Number with 4 digits after the decimal point : " + integerNumber.ToString("F4", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 5
Console.WriteLine("The Number with 5 digits after the decimal point : " + integerNumber.ToString("F5", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 6
Console.WriteLine("The Number with 6 digits after the decimal point : " + integerNumber.ToString("F6", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 7
Console.WriteLine("The Number with 7 digits after the decimal point : " + integerNumber.ToString("F7", CultureInfo.InvariantCulture));
}
}
}
Output
Apply a different Fixed-Point specifier in a positive Double number.
If the number is of double type then the Format-Point specifier will also round off that number and give the result with the decimal point in different formats.
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double doubleNumber;
doubleNumber = 18934.1879;
//by default after the decimal point the number digits are 2
Console.WriteLine("The Fixed-point format specifier for an Double value is :"+doubleNumber.ToString("F", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 3
Console.WriteLine("The Number with 3 digits after the decimal point : " + doubleNumber.ToString("F3", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 4
Console.WriteLine("The Number with 4 digits after the decimal point : " + doubleNumber.ToString("F4", CultureInfo.InvariantCulture));
//here the number digits after the decimal point is 5
Console.WriteLine("The Number with 5 digits after the decimal point : " + doubleNumber.ToString("F5", CultureInfo.InvariantCulture));
}
}
}
Output
Apply different Fixed-Point specifier in a Negative integer number.
It's the same as the positive integer value, the difference is only the negative sign that precedes the number.
Apply different Fixed-Point specifier in a Negative Double number.
It's the same as the double integer value, the difference is only the negative sign that precedes the number.
Numbers with no decimal point
If I do not want a decimal point and all the digits after the decimal point, only the number, then for this I use the Fixed-format as "F0" as:
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
//Define a decimal number
double doubleNumber = 18934.1879;
//Define a interger number
int integerNumber = 17843;
//The integer number with no decimal point
Console.WriteLine("The Fixed-point format specifier for an Integer value is :"+ integerNumber.ToString("F0", CultureInfo.InvariantCulture));
//The double number with no decimal point
Console.WriteLine("The Fixed-point format specifier for an Double value is :" + doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
}
}
}
Output
Apply Different Culture In Fixed-Point specifier
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double doubleNumber;
doubleNumber = 18934.1879;
//Give the result number including commam ,
Console.WriteLine("Culture es-ES : " +doubleNumber.ToString("F3",CultureInfo.CreateSpecificCulture("es-ES")));
//Give the result number including decimal point .
Console.WriteLine("Culture en-US : " + doubleNumber.ToString("F3", CultureInfo.CreateSpecificCulture("en-US")));
//Give the result number including commam ,
Console.WriteLine("Culture de-DE : " + doubleNumber.ToString("F3", CultureInfo.CreateSpecificCulture("de-dE")));
}
}
}
Output
Summary
In this article, I explained how to use the Fixed-Point format specifier in C#.