Illustrates formatting numbers : Number Format « Development Class « C# / C Sharp






Illustrates formatting numbers

Illustrates formatting numbers
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example2_15.cs illustrates formatting numbers
*/

public class Example2_151
{

  public static void Main()
  {

    // formatting integers
    int myInt = 12345;
    int myInt2 = 67890;
    System.Console.WriteLine("myInt = {0, 6}, myInt2 = {1, 5}",
      myInt, myInt2);
    System.Console.WriteLine("myInt using 10:d = {0, 10:d}",
      myInt);
    System.Console.WriteLine("myInt using 10:x = {0, 10:x2}",
      myInt);

    // formatting floating-point numbers
    double myDouble = 1234.56789;
    System.Console.WriteLine("myDouble using 10:f3 = {0, 10:f3}",
      myDouble);
    float myFloat = 1234.56789f;
    System.Console.WriteLine("myFloat using 10:f3 = {0, 10:f3}",
      myFloat);
    decimal myDecimal = 1234.56789m;
    System.Console.WriteLine("myDecimal using 10:f3 = {0, 10:f3}",
      myDecimal);
    System.Console.WriteLine("myFloat using 10:e3 = {0, 10:e3}",
      myFloat);
    System.Console.WriteLine("myFloat using 10:p2 = {0, 10:p2}",
      myFloat);
    System.Console.WriteLine("myFloat using 10:n2 = {0, 10:n2}",
      myFloat);
    System.Console.WriteLine("myFloat using 10:g2 = {0, 10:g2}",
      myFloat);

    // formatting currency values
    decimal myMoney = 15123.45m;
    System.Console.WriteLine("myMoney using 10:c2 = {0, 10:c2}",
      myMoney);

  }

}

           
       








Related examples in the same category

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