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
Different Strings Format for numbers
Posted By Sarin on Mar 10, 2012     RSS Feeds     Latest Hinduism news
3838 Views

Different Strings Format for numbers

Specifier Description Effect of Precision Specifier
C or c Formats the number as a monetary value including the correct number of decimal places and the appropriate currency symbol for the user's local setting Specifies a fixed number of decimal places.
D or d Formats integers only as simple whole numbers. Specifies the minimum number of digits.  Leading zeroes are added if required.
E Formats numbers using exponential notation <https://en.wikipedia.org/wiki/Exponential_notation>.  The resultant string includes an upper case letter 'E'.Specifies a fixed number of decimal places for the mantissa.  If omitted, six decimal places are used. 
e Formats numbers using exponential notation.  The resultant string includes an lower case letter 'e'. Specifies a fixed number of decimal places for the mantissa.  If omitted, six decimal places are used.
F or f Formats numbers using fixed point notation. Specifies a fixed number of decimal places.
G or g Formats numbers using either exponential or fixed point notation, whichever produces the shortest string.  The actual results vary according to the data type being converted and whether a precision specifier is used. See 'e', 'E' and 'F or f'.
N or n Formats numbers using fixed point notation with thousands separators. Specifies a fixed number of decimal places.
P or p Formats numbers using percentage notation.  For example, 0.25 is formatted as 25%. Specifies a fixed number of decimal places.
R or r Formats numbers using Round-Trip Format.  This is a special format that ensures that the string generated can be converted back to the original number using Parse methods. Unused.
X Converts numbers into a string representation of their hexadecimal value.  The digits 0 to 9 and A to F are used. Specifies the minimum number of digits.  Leading zeroes are added if required.
x Converts numbers into a string representation of their hexadecimal value.  The digits 0 to 9 and a to f are used. Specifies the minimum number of digits.  Leading zeroes are added if required

  

Snapshot showing commonly used string formats
Formatting Currency:
  double dNumber = 123456.789;
           
// display two digit after decimal point which is default
           
Console.WriteLine(dNumber.ToString("C", CultureInfo.CurrentCulture));
           
// output: $ 123,456.78
  
           
// display four digit after decimal point which is default
           
Console.WriteLine(dNumber.ToString("C6", CultureInfo.CurrentCulture));
           
Console.ReadLine();
           
// output: $ 123,456.789000

Formatting Integers
int iNumber = 123;
  
           
Console.WriteLine(iNumber.ToString("D"));
           
// output: 123
  
           
// append zero at left till the number become of six digits
           
Console.WriteLine(iNumber.ToString("D6"));
           
// output: 000123
  
            iNumber = -12345;
           
Console.WriteLine(iNumber.ToString("D"));
           
// output: -12345
  
           
// append zero at left till the number become of seven digits
           
Console.WriteLine(iNumber.ToString("D7"));
           
// output: -0012345

Formats numbers using exponential notation <https://en.wikipedia.org/wiki/Exponential_notation>
       Console.WriteLine(dNumber.ToString("E", CultureInfo.InvariantCulture));
           
// output: 1.234568E+005
  
           
Console.WriteLine(dNumber.ToString("E5", CultureInfo.InvariantCulture));
           
// output: 1.23457E+005
  
           
Console.WriteLine(dNumber.ToString("e6", CultureInfo.InvariantCulture));
           
// output: 1.234568e+005

Format number to fixed point notation
dNumber = 9876.5432;
           
// display two digit after decimal point which is default
           
Console.WriteLine(dNumber.ToString("F", CultureInfo.InvariantCulture));
           
// output: 9876.54
  
           
// display three digit after decimal point
           
Console.WriteLine(dNumber.ToString("F3", CultureInfo.InvariantCulture));
           
// output:  9876.543
  
           
// do not display decimal point
            dNumber = -9876.5432;
           
Console.WriteLine(dNumber.ToString("F0", CultureInfo.InvariantCulture));
           
// output: -9877
  
// display five digit after comma (comma instead of decimal point is format for france)
            Console.WriteLine(dNumber.ToString("F5",
                             
CultureInfo.CreateSpecificCulture("fr-FR")));
           
// output: -9876,54320    

Format number using either exponential notation <https://en.wikipedia.org/wiki/Exponential_notation> or fixed point notation
dNumber = 9876.5432;
           
// display the number in the culture indepenedent format
           
Console.WriteLine(dNumber.ToString("G", CultureInfo.InvariantCulture));
           
// output: 9876.5432;
  
           
// display the number in the swedish culture format
           
Console.WriteLine(dNumber.ToString("G",CultureInfo.CreateSpecificCulture("sv-SE")));
           
// output: 9876,5432
  
           
// display the first seven digit of the number
           
Console.WriteLine(dNumber.ToString("G7", CultureInfo.InvariantCulture));
           
// output: 9876.543
  
            dNumber = .0000965;
           
// since there is more than 4 zero after decimalpoint, number will be displayed in exponential format
           
Console.WriteLine(dNumber.ToString("G", CultureInfo.InvariantCulture));
           
// output: 9.65E-05
  
           
// More than 4 zero after decimalpoint, So number will be displayed in exponential format as per the swedish culture
           
Console.WriteLine(dNumber.ToString("G",         CultureInfo.CreateSpecificCulture("sv-SE")));
           
// output: 9,65E-05
  
             dNumber =
Math.E;
             
// display the exponential value upto five digits
           
Console.WriteLine(dNumber.ToString("G5", CultureInfo.InvariantCulture));
           
// output: 2.7183    
  

Formats numbers using fixed point notation with thousands separators.
  
dNumber = -74965.7249;
           
// display the exponential value upto five digits
           
Console.WriteLine(dNumber.ToString("N", CultureInfo.InvariantCulture));
           
// Displays -74,965.72
  
           
// display the exponential value upto five digits
           
Console.WriteLine(dNumber.ToString("N1", CultureInfo.CreateSpecificCulture("ru-RU")));
           
// Displays -74 965,7
  
           
// display the exponential value upto five digits
           
Console.WriteLine(dNumber.ToString("N5", CultureInfo.InvariantCulture));
           
// Displays 74,956.72490  

Formats numbers using percentage notation
  
dNumber = .7857302;
           
// display the number in percentage format independent of culture
           
Console.WriteLine(dNumber.ToString("P", CultureInfo.InvariantCulture));
           
// output: 78.57 %
  
           
// display the number in percentage format of denmark culture
           
Console.WriteLine(dNumber.ToString("P", CultureInfo.CreateSpecificCulture("de-DE")));
           
// output: 78,57%  
  
           
// display the number in percentage format upto one decimal point independent of culture
           
Console.WriteLine(dNumber.ToString("P1", CultureInfo.InvariantCulture));
           
// output: 78.6%  

  
Formats numbers using Round-Trip notation
dNumber = Math.PI;
   
//  display the number till the first two digit is repeated again. 31 in this case
   
Console.WriteLine(dNumber.ToString("r"));
   
// Displays 3.1415926535897931
  
   
//  display the number till the first two digit is repeated again using french culture
     
Console.WriteLine(dNumber.ToString("r",CultureInfo.CreateSpecificCulture("fr-FR")));
   
// Displays 3,1415926535897931

  
Formats numbers using hexadecimal notation
  
  iNumber = 0x2045e;
           
// Display the hexadecimal number. Since case of format specifier is lowercase, value is also shown in lowercase
           
Console.WriteLine(iNumber.ToString("x"));
           
// Displays 2045e
  
           
// Display the hexadecimal number. Since case of format specifier is Uppercase, value is also shown in Uppercase
           
Console.WriteLine(iNumber.ToString("X"));
           
// Displays 2045E
  
           
// Display the hexadecimal number. Value will be shown till 8 digits
           
Console.WriteLine(iNumber.ToString("X8"));
           
// Displays 0002045E
  
            iNumber = 123456789;
           
// Display the hexadecimal equivalent of the integer
           
Console.WriteLine(iNumber.ToString("X"));
           
// Displays 75BCD15

here

Share this to your friends. One of your friend is waiting for your share.
Related Articles
Advanced Datetime Format for string
Different Strings Format for numbers
Create property class of database table
Science in Hinduism-Place value and Decimal number system
Commonly used string Functions
Different Strings Format for Date Time
Capitalise first character of every word
Parse comma separated input string in SQL
SQL-Removing numbers in a string
Call Codebehind method from GridView ItemTemplate Eval function

Post Comment