C#
  Home arrow C# arrow Page 2 - C# Simplified, part 3
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Simplified, part 3
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 11
    2005-05-17

    Table of Contents:
  • C# Simplified, part 3
  • Displaying numbers in different formats
  • Display date and time in different formats
  • Arrays
  • Using Array.Rank
  • Enumerations

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# Simplified, part 3 - Displaying numbers in different formats


    (Page 2 of 6 )

    C# enables you to display numbers in different ways with the help of Format Specifiers. For example, you can display numbers with a currency symbol or with decimal places. These specifiers are enclosed within curly braces followed by the variable name of the specific value.

    In Listing 3.2, a variable named i is declared and its value is displayed in different ways using a set of format specifiers. I have given necessary explanations as comments.

    Listing 3.2

    using System;

    class NumberFormat

    {

    public static void Main()

    {

    int i = 7777;

    // Returns the exact currency value with the symbol ($)

    Console.WriteLine("{0:C}",i);

    // Returns the Number converted to a string of decimal digits

    Console.WriteLine("{0:D}",i);

    // Returns the Expotential value

    Console.WriteLine("{0:E}",i);

    // Returns the fixed point string representation of the number

    Console.WriteLine("{0:F}",i);

    // Returns the most compact form of fixed point number

    Console.WriteLine("{0:G}",i);

    // Returns the number in the d,ddd,ddd.ddd format

    Console.WriteLine("{0:N}",i);

    // Returns the Uppercase hexadecimal digit of the converted number

    Console.WriteLine("{0:X}",i);

    // Returns the Lowercase hexadecimal digit of the converted number

    Console.WriteLine("{0:x}",i);

    }

    }

    The final output of the above program will look like Figure 3.2.

    Figure 3.2

    Format Specifiers combined with numbers are called Precision Specifiers. For example, if you use a format like C6, you will get six zeros appended after the decimal position of the number. Listing 3.3 shows a modified version of listing 3.2.

    Listing 3.3

    using System;

    class NumberFormatPrecision

    {

    public static void Main()

    {

    int i = 7777;

    // Returns the exact currency value with the symbol ($)

    Console.WriteLine("{0:C5}",i);

    // Returns the Number converted to a string of decimal digits

    Console.WriteLine("{0:D5}",i);

    // Returns the Expotential value

    Console.WriteLine("{0:E5}",i);

    // Returns the fixed point string representation of the number

    Console.WriteLine("{0:F5}",i);

    // Returns the most compact form of fixed point number

    Console.WriteLine("{0:G5}",i);

    // Returns the number in the d,ddd,ddd.ddd format

    Console.WriteLine("{0:N5}",i);

    // Returns the Uppercase hexadecimal digit of the converted number

    Console.WriteLine("{0:X5}",i);

    // Returns the Lowercase hexadecimal digit of the converted number

    Console.WriteLine("{0:x5}",i);

    }

    }

    The output of the above program looks like Figure 3.3.

    Figure 3.3

    More C# Articles
    More By Anand Narayanaswamy


     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT