Use String.Format() to format a value : Number Format « Development Class « C# / C Sharp






Use String.Format() to format a value

Use String.Format() to format a value
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use String.Format() to format a value.  
  
using System;  
  
public class FormatDemo1 {  
  public static void Main() {  
    double v = 17688.65849;  
    double v2 = 0.15;  
    int x = 21;  
   
    string str = String.Format("{0:F2}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:N5}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:e}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:r}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:p}", v2);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:X}", x);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:D12}", x);  
    Console.WriteLine(str);  
 
    str = String.Format("{0:C}", 189.99); 
    Console.WriteLine(str); 
  }  
}

           
       








Related examples in the same category

1.Format an enumerationFormat an enumeration
2.A closer look at Format()A closer look at Format()
3.Use ToString() to format valuesUse ToString() to format values
4.Using custom formatsUsing custom formats
5.Numeric Formatting:Custom Format Strings:Decimal PointNumeric Formatting:Custom Format Strings:Decimal Point
6.Numeric Formatting:Custom Format Strings:Digit or Space PlaceholderNumeric Formatting:Custom Format Strings:Digit or Space Placeholder
7.Numeric Formatting:Custom Format Strings:Digit or Zero PlaceholderNumeric Formatting:Custom Format Strings:Digit or Zero Placeholder
8.Numeric Formatting:Custom Format Strings:Escapes and LiteralsNumeric Formatting:Custom Format Strings:Escapes and Literals
9.Numeric Formatting:Custom Format Strings:Exponential NotationNumeric Formatting:Custom Format Strings:Exponential Notation
10.Numeric Formatting:Custom Format Strings:Group SeparatorNumeric Formatting:Custom Format Strings:Group Separator
11.Numeric Formatting:Custom Format Strings:Number PrescalerNumeric Formatting:Custom Format Strings:Number Prescaler
12.Numeric Formatting:Custom Format Strings:Percent NotationNumeric Formatting:Custom Format Strings:Percent Notation
13.Numeric Formatting:Custom Format Strings:Section SeparatorNumeric Formatting:Custom Format Strings:Section Separator
14.Numeric Formatting:Standard Format Strings:CurrencyNumeric Formatting:Standard Format Strings:Currency
15.Numeric Formatting:Standard Format Strings:DecimalNumeric Formatting:Standard Format Strings:Decimal
16.Numeric Formatting:Standard Format Strings:Fixed-PointNumeric Formatting:Standard Format Strings:Fixed-Point
17.Numeric Formatting:Standard Format Strings:GeneralNumeric Formatting:Standard Format Strings:General
18.Numeric Formatting:Standard Format Strings:HexadecimalNumeric Formatting:Standard Format Strings:Hexadecimal
19.Numeric Formatting:Standard Format Strings:NumberNumeric Formatting:Standard Format Strings:Number
20.Numeric Formatting:Standard Format Strings:Scientific (Exponential)Numeric Formatting:Standard Format Strings:Scientific (Exponential)
21.Illustrates formatting numbersIllustrates formatting numbers