Results 1 to 6 of 6

Thread: Formatting output number in c#

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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).

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    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);    
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    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);

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    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 );
    }

Similar Threads

  1. how to increase the number of decimal digits in JAVA program output?
    By Dennis Racket in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 02:38 PM
  2. Replies: 6
    Last Post: 16-08-2011, 10:51 PM
  3. Formatting MS Excel 2010 vs. Formatting MS Excel 2003
    By Dalajit in forum Windows Software
    Replies: 6
    Last Post: 12-08-2011, 10:19 PM
  4. Replies: 5
    Last Post: 21-03-2011, 10:24 AM
  5. Replies: 4
    Last Post: 01-02-2011, 01:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,417,161.57807 seconds with 16 queries