user
2
Q:

c# type conversion


// --------------------- TYPE CONVERSION --------------------- //

// Implicit conversion
float varFloat = 12.7f;
double varDouble = varFloat;   // converts float => double


// Explicit conversion (casting)
double varDouble = 2.3;
int varInt = (int) varDouble;        // converts double => int


// Type convertion: number to string
int varInt = 19;
string varString = varInt.ToString();    // converts int => string


// Type convertion: string to number
string varString = "89";   
int varInt = int.Parse(varString);         // converts string => int
double varDouble = double.Parse(varString);  // converts string => double
//OR
int varInt = Convert.ToInt32(varString);
double varDouble = Convert.ToDouble(varString);
 
1
double myDouble = 9.78;
int myInt = (int) myDouble;    // Manual casting: double to int

Console.WriteLine(myDouble);   // Outputs 9.78
Console.WriteLine(myInt);      // Outputs 9
1

New to Communities?

Join the community