Formatting output number in c#
Hello to all,
I am second year Computer Science student. I want to format output number using c#. It means that output number always has 2 digits. In simple word if int i = 2, then Console.WriteLine(i) has to produce number like 22. Please help me in formatting output number in c# .
Thanks in advanced.
Re: Formatting output number in c#
You have to use Custom Numeric Format Strings to format number in c#. This contains one or more custom numeric specifiers, to define how to format numeric data. Custom numeric format strings are part of some ToString method of all numeric types. It is very easy to use and implement. There are tow type of to string methods like ToString(String) and ToString(String, IFormatProvider).
Re: Formatting output number in c#
You have to use following code to Format output number in c#.
Code:
int nums = 10;
Console.WriteLine(nums.ToString("0s#"));
Console.ReadLine();
In above code the string that is passed to ToString() method has the meaning as follows.
In above code "0" is used like place holder. This is used to replace all digits with existing digits.
In the above code # is used like digit place holder.
Re: Formatting output number in c#
You have to use following code to fix this problem. In the following code I have use for loop to continue the loop for hundread time. In the Console.WriteLine() method I have use {0:00} to get outout in two numbers only. It is very simple to do this.
Code:
for (int k = 0; k < 100; k++)
{
Console.WriteLine("{0:00}", k);
}
Re: Formatting output number in c#
As per my information you have to use {0:00} in your any of the string method like string.Format() or Console.String() method to get proper output. It is very simple to do this. You have to use "Custom number formatting" concept to do this. To show only two numbers you have to use following code.
Code:
int a = 2;
string outputa = string.Format("{0:00}", a);
Console.WriteLine(outputa);
Re: Formatting output number in c#
Formatting output number in c# is very simple task. You have to just use Custom Numeric Format Strings in your code to do this. You have to just use text_line.Lengt method to get proper output. It is very simple to do this. Using following code you have to get output in proper format.
Code:
while (( texts_lines = freaders.ReadLines() ) != null )
{
if ( texts_lines.Lengths == 0 )
{
Consoles.WriteLine();
continues;
}
Console.WriteLine( "{0} ({2}): {1}",
line_cnts++, texts_line, texts_line.Length );
}