Wednesday, August 21, 2013

DataType Conversion in MVC Razor

The following table lists some common conversion and test methods for variables.
MethodDescriptionExample
AsInt(),
IsInt()
Converts a string that represents a whole number (like "593") to an integer.
var myIntNumber = 0;
var myStringNum = "539";
if(myStringNum.IsInt()==true){
    myIntNumber = myStringNum.AsInt();
}
AsBool(),
IsBool()
Converts a string like "true" or "false" to a Boolean type.
var myStringBool = "True";
var myVar = myStringBool.AsBool();
AsFloat(),
IsFloat()
Converts a string that has a decimal value like "1.3" or "7.439" to a floating-point number.
var myStringFloat = "41.432895";
var myFloatNum = myStringFloat.AsFloat(); 
AsDecimal(),
IsDecimal()
Converts a string that has a decimal value like "1.3" or "7.439" to a decimal number. (In ASP.NET, a decimal number is more precise than a floating-point number.)
var myStringDec = "10317.425";
var myDecNum = myStringDec.AsDecimal(); 
AsDateTime(),
IsDateTime()
Converts a string that represents a date and time value to the ASP.NET DateTime type.
var myDateString = "12/27/2012";
var newDate = myDateString.AsDateTime();
ToString()Converts any other data type to a string.
int num1 = 17;
int num2 = 76;
// myString is set to 1776
string myString = num1.ToString() +
  num2.ToString();

No comments :