Convert between Simple Types in CSharp

Description

The following code shows how to convert between Simple Types.

Example


using System;//from  ww  w .j  ava2 s  . c om

public class MainClass {
    public static void Main() {

        double d = 2.9;
        Console.WriteLine((int)d);                   // double-->int; prints 2 
        Console.WriteLine((int)(-d));                // double-->int; prints -2 
        uint seconds = (uint)(24 * 60 * 60);         // int-->uint 
        double avgSecPerYear = 365.25 * seconds;     // I uint-->double 
        float f = seconds;                           // IL uint-->float 
        long nationalDebt1 = 99999999999999;
        double perSecond = 99999.14;
        decimal perDay = seconds * (decimal)perSecond;              // I uint-->decimal 
        double nd2 = nationalDebt1 + (double)perDay; // decimal-->double 
        long nd3 = (long)nd2;                        // double-->long 
        float nd4 = (float)nd2;                      // double-->float 

    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception