xxxxxxxxxx
string strValue = "3.14159";
float floatValue;
// using Parse method
floatValue = float.Parse(strValue);
// using TryParse method
if (float.TryParse(strValue, out floatValue))
{
// conversion successful
}
else
{
// conversion failed
}
the Parse method is used to convert the string "3.14159" to a float. The TryParse method is used to convert the string, but it returns a Boolean value indicating whether the conversion was successful or not. The converted float value is stored in the floatValue variable.
xxxxxxxxxx
string s1 = "1.0"
float f1 = float.Parse(s1);
// Change the delimiter - (The default delimiter is based on your current CultureInfo)
var ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.NumberDecimalSeparator = ",";
string s2 = "1,1";
float f2 = float.Parse(s2, ci);
xxxxxxxxxx
int integerValue = 42;
float floatValue = (float)integerValue;
Console.WriteLine(floatValue);