Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Formatting output number in c#

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 10-03-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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).
Reply With Quote
  #3  
Old 10-03-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
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.
Reply With Quote
  #4  
Old 10-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
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);    
}
Reply With Quote
  #5  
Old 10-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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);
Reply With Quote
  #6  
Old 10-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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 );
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Formatting output number in c#"
Thread Thread Starter Forum Replies Last Post
how to increase the number of decimal digits in JAVA program output? Dennis Racket Software Development 2 3 Weeks Ago 02:38 PM
Number of Installs reported by Console and the Number of Sales for Paid Apps doesn’t match? PhoebeM Portable Devices 6 16-08-2011 11:51 PM
Formatting MS Excel 2010 vs. Formatting MS Excel 2003 Dalajit Windows Software 6 12-08-2011 11:19 PM
Vodafone gets the maximum number of users after the mobile number portability Antariksh Portable Devices 5 21-03-2011 11:24 AM
Limit number of displayed characters while maintaining bold and italic formatting in ASP Wayur89 Software Development 4 01-02-2011 01:43 PM


All times are GMT +5.5. The time now is 12:02 PM.