Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment


Locations of visitors to this page


Latest Articles


Move to top
Advanced Strings Format for numbers
Posted By Sarin on Mar 14, 2012     RSS Feeds     Latest Hinduism news
3480 Views

Advanced Numeric String Formats

Zero Placeholder
 If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string.  
in case of decimal numbers, The Zero Placeholder causes the value to be rounded to the nearest rounded digit. Ex, formatting 44.6 with "00" would result in the value 45.
  
  double dNumber = 6.7;
  
           
// Added zero at end so as to satisfy the format
           
Console.WriteLine(dNumber.ToString("0.00", CultureInfo.InvariantCulture));
           
// output: 6.70
  
           
// Added zero at beginning so as to satisfy the format
           
Console.WriteLine(dNumber.ToString("00.00", CultureInfo.InvariantCulture));
           
// output: 06.70
  
            dNumber = .67;
           
// Since only one zero after decimal point, value will be rounded off
           
Console.WriteLine(dNumber.ToString("0.0", CultureInfo.InvariantCulture));
           
// output: .7
  
            dNumber = 987654321;
           
// show value in thousand seperator format
           
Console.WriteLine(dNumber.ToString("0,0", CultureInfo.InvariantCulture));
           
// output: 987,654,321  
  
           
// show value in thousand seperator format in german format where seperator is shown as space
           
Console.WriteLine(dNumber.ToString("0,0", CultureInfo.CreateSpecificCulture("fr-FR")));
           
// output: 987 654 321
  
            dNumber = 987654.3210;
           
// show value in thousand seperator format with digits after decimal point
           
Console.WriteLine(dNumber.ToString("0,0.00", CultureInfo.InvariantCulture));

The pound Placeholder  <javascript:void(0)>
In pound format specifier, if for every pound sign, corresponding digit appears in the input string, then that digit is copied to the result string. Otherwise, nothing is shown on that position of the result string.  
It defines the position of a digit within the resultant formatted string and causes rounding after the decimal point.  
Unlike the zero Placeholder specified, this specifier does not display an additional zero even if the corresponding # it is present in the format string
Similar to zero Placeholder, The "#" format specifier  the value to be rounded to the nearest digit preceding the decimal. For example, formatting 44.5 with "##" would result in the value 45.
  
  dNumber = 4.5;
           
// Additional zero is not shown in place of #
           
Console.WriteLine(dNumber.ToString("#.##", CultureInfo.InvariantCulture));
           
// output: 4.5
  
            dNumber = 440;
           
// Additional zero is not shown in place of #
           
Console.WriteLine(dNumber.ToString("#####"));
           
// output: 440
  
            dNumber = 440365;
           
// each # symbol is replaced by the corresponding digit of the input string.
           
Console.WriteLine(dNumber.ToString("[##-##-##]"));
           
// output: [44-03-65]
  
            dNumber = 4403652310;
           
// each # symbol is replaced by the corresponding digit of the input string.Additional characters is displayed as it is
           
Console.WriteLine(dNumber.ToString("(###) a###-b####"));
           
// output: (440) a365-b2310

Decimal Point Placeholder
This is actually the combination of zero and pound placeholder and can be used to display the number of digits required both before and after a decimal point.  Although the decimal point placeholder character is always a full-stop or period character (.), the actual character in the resultant string is dependent upon the user's local settings
  
  dNumber = 6.7;
             
           
// Added zero at beginning so as to satisfy the format
           
Console.WriteLine(dNumber.ToString("00.00", CultureInfo.InvariantCulture));
           
// output: 06.70
  
dNumber = .067;
           
// combining # and zero to show the resultant string in french culture format
           
Console.WriteLine(dNumber.ToString("#0.##%", CultureInfo.CreateSpecificCulture("fr-FR")));
           
// output: 6,7%
  
            dNumber = 670;
           
// combining # and zero to show the resultant string.E+ in format string shows result in exponential format
           
Console.WriteLine(dNumber.ToString("0.###E+0", CultureInfo.InvariantCulture));
           
// output: 6.7E+5

Thousands Separator Placeholder
The thousands separator placeholder is represented as a comma (,).  If a comma is included in the string format to the left of any decimal point and between zero or digit placeholders then thousands separators are inserted into the resultant string.  Each Thousands Separator character divides the number by one thousand before applying the formatting. Although the thousands placeholder character is always a comma, the actual character in the resultant string is dependent upon the user's local settings.  
  
double value = 982145367852.910;
           
// Show string in thousand seperator format
           
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
           
// output: 982,145,367,853  
  
           
// Since there is neither # or 0 between last comma, resultant string digits will not be shown till two comma
           
Console.WriteLine(value.ToString("#,#0,,", CultureInfo.InvariantCulture));
           
// output: 982,145
  
           
// resultant string digits is not shown for last thousand seperator and result is rounded off.
           
Console.WriteLine(value.ToString("#,#0,#,", CultureInfo.InvariantCulture));
           
// output: 982,145,368  
  
  
Percentage Placeholder
  
The percentage placeholder (%) is used when displaying percentage values.  The placeholder performs two actions.  Firstly it identifies the position in the resultant string where the percentage symbol should appear.  Secondly, percent sign (%)causes a number to be multiplied by 100 before it is formatted.  
  
dNumber = .0762;
           
// Number is multiplied by 100 and shown with prcent sign.
           
Console.WriteLine(dNumber.ToString("#0.##%", CultureInfo.InvariantCulture));
           
// output:  7.62%  
  
            dNumber = 762.0452;
           
// number is multiplied by 100 twice, once before decimal point and once after decimal point.
           
//however you will never use this approach practically
           
Console.WriteLine(dNumber.ToString("#%.0%", CultureInfo.InvariantCulture));
           
// output:  7620452%.0%  
  
  Custom Exponential Notation
In Custom Exponential Notation, The first character will be always 'E' or 'e'   to specify that exponential notation is to be used.  This is then followed by + or - sign, immediately followed by one or more zeroes to represent the input value. A plus symbol (+) indicates that the sign for the exponent value should always be included in the resultant string.  A minus (-) indicates that the sign character should only appear for negative exponent values.
  
     double value = 76125.10200;
           
// Number shown in exponential form arranged using hash and zero placeholder
           
Console.WriteLine(value.ToString("0.#0#E+0", CultureInfo.InvariantCulture));
           
// output: 7.613E+4
  
           
//  Number shown in exponential swedish culture format
           
Console.WriteLine(value.ToString("0.##E+000", CultureInfo.CreateSpecificCulture("sv-SE")));
           
// output: 7,61E+004
  
           
Console.WriteLine(value.ToString("0.#E-000", CultureInfo.InvariantCulture));
           
// output: 7.6E004

Literal Characters
Often numeric values need to be formatted to include non-numeric characters.  This can be achieved by inserting the characters, you want to display, in the string format. To insert special characters, enclose them in apostrophes (') or quotes (").  
dNumber = 76125109;
           
// extra character is shown as it is and each 0 replaced by corresponding digit in input string
           
Console.WriteLine(dNumber.ToString("00<00-0>00", CultureInfo.InvariantCulture));
           
// output: 761<25-1>09
  
           
// Zero and hash placeholder format the input string as per their fucntion. Additional character is shown as it is
           
Console.WriteLine(dNumber.ToString("#[0]#/#'#''0'", CultureInfo.CreateSpecificCulture("sv-SE")));
           
// output: 76125[1]0/9#0

  
Escape Characters
Normally, The "#", "0", ".", ",", "%", and "�" symbols in a format string are interpreted as format specifier rather than as literal characters.  To prevent this character to be used as format specifier, you can precede it with a backslash, which is the escape character. The escape character signifies that the following character is a character literal that should be included in the result string unchanged with no formatting applied.
C# compiler treats the double backslash as a single backslash. Hence to insert a backslash character into to final formatted string, either four backslashes must be included in the string format or use the @ operator
  
dNumber = 761251.09;
           
// for double slash found at start and end, character following slash not treated as format specifier
           
Console.WriteLine(dNumber.ToString("\\.\\ #00 Rupees and .00 paisa \\.\\."));
           
// output: .. 761251 Rupees and .09 paisa ..
  
           
// Four slash combined to form single slash, zero preceeding paisa is shown as it is since it is followed by double slash
           
Console.WriteLine(dNumber.ToString("\\\\\\\\ ##0 Rupees and .0 \\0 paisa \\\\\\\\"));
           
// output: \\ 761251 Rupees and .10 paisa \
  
           
// when @ operator used, two slash combined to show it as single slash
           
Console.WriteLine(dNumber.ToString(@"\\\\ ##0 Rupees and .0 \\0 paisas \\\\"));
           
// output: \\ 761251 Rupees and .10 paisa \\
  
  
Section Separator Character
This formatter allows different formatting to be used for positive, negative and zero values.  If a single semicolon (;) character is included in the format string, then the format to the left of the semicolon is used for positive values and the format to the right for negative numbers.  Adding a second section separator indicates that the third section is to be applied to zero values.  If the second section is omitted with no characters in between two semicolons then the positive and negative formats will both use the formatting from the first section.
  
dNumber = 761.09;
           
// number is postive, so number is shown in 0.0 format
           
Console.WriteLine(dNumber.ToString("0.0;(0.0)"));         // Outputs "1.0"
           
// output: 761.1
  
            dNumber = -761.09;
           
// number is megative, so number is shown in #0.00 format
           
Console.WriteLine(dNumber.ToString("0.0;#0.00"));       // Outputs "-1.0"
           
// output: 761.09
  
           
// number is shown negative and displayed in 0.0 format
           
Console.WriteLine(dNumber.ToString("0.0;;#0.00"));       // Outputs "-1.0"
           
Console.ReadLine();
           
// output: -761.1  


Snapshot showing commonly used string formats



here

Share this to your friends. One of your friend is waiting for your share.
Share on Google+ Share on Tumblr Print Browser Favorite
Related Articles
Different Strings Format for Date Time
Generating unique random numbers and date
Call Codebehind method from GridView ItemTemplate Eval function
Commonly used string Functions
Science in Hinduism-Place value and Decimal number system
Create property class of database table
Ways of generating random password or string
Parse comma separated input string in SQL
Advanced Strings Format for numbers
SQL-Removing numbers in a string

Post Comment