I need to convert a string to a float....
-
Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way
Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));
Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??
-
Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way
Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));
Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??
I typically use float.TryParse(string, out float result); This returns true if successful and false if not. HTH
Jack of all trades, master of none, though often times better than master of one.
-
I typically use float.TryParse(string, out float result); This returns true if successful and false if not. HTH
Jack of all trades, master of none, though often times better than master of one.
Hi, Thanks for that float.TryParse(), will I be able to get out as a floating point?..
-
Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way
Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));
Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??
Are you sure it compiles?
String
doesn't have aConvertTo
method, and you can't pass theDecimal
type as an argument like that. Assuming the text is a valid number, thenConvert.ToSingle(rtdData.Text)
should give you afloat
back. OrConvert.ToDecimal(rtdData.Text)
would give you adecimal
, which you would then have to cast to store in afloat
variable. But these methods will throw an exception if the text is not a valid number. It would be better to usefloat.TryParse
/decimal.TryParse
so that you can notify the user if the value can't be parsed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Are you sure it compiles?
String
doesn't have aConvertTo
method, and you can't pass theDecimal
type as an argument like that. Assuming the text is a valid number, thenConvert.ToSingle(rtdData.Text)
should give you afloat
back. OrConvert.ToDecimal(rtdData.Text)
would give you adecimal
, which you would then have to cast to store in afloat
variable. But these methods will throw an exception if the text is not a valid number. It would be better to usefloat.TryParse
/decimal.TryParse
so that you can notify the user if the value can't be parsed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Okay, I will give that a go... It hasn't crashed yet!!
-
Hi, Thanks for that float.TryParse(), will I be able to get out as a floating point?..
Yes:
if (float.TryParse(myTextBox.Text, out float result))
{
Console.WriteLine(result);
}"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way
Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));
Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??
-
Old question now solved!
-
With this extension we can parse strings directly to a numeric type
static bool Fail<T>(this object _, out T item) where T : struct
{
item = default;
return false;
}static bool Pass<T>(this object obj, out T item) where T : struct
{
Type conversionType = typeof(T);
item = (T)Convert.ChangeType(obj, conversionType);
return true;
}const string System_Int16 = "System.Int16";
const string System_UInt16 = "System.UInt16";
const string System_Int32 = "System.Int32";
const string System_Int64 = "System.Int64";
const string System_UInt32 = "System.UInt32";
const string System_Single = "System.Single";
const string System_Double = "System.Double";
const string System_UInt64 = "System.UInt64";
const string System_Byte = "System.Byte";
const string System_Decimal = "System.Decimal";
const string System_SByte = "System.SByte";
const string System_Numerics_BigInteger = "System.Numerics.BigInteger";
public static bool TryParse<T>(this string value, out T item) where T : struct
{
var conversionType = typeof(T);
var floatType = typeof(float);
var doubleType = typeof(double);
var decimalType = typeof(decimal);
if(conversionType != floatType && conversionType != doubleType
&& conversionType != decimalType && value.Contains('.'))
{
//we're not converting to float, double or decimal
//there's no rounding, we just drop everything from the "."
value = value.Remove(value.LastIndexOf('.'));
}
return conversionType.FullName switch
{
System_Int16 => short.TryParse(value, out var result) ?
result.Pass(out item) : value.Fail(out item),System_UInt16 => ushort.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Decimal => decimal.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Int32 => int.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Int64 => long.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_UInt64 => ulong.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_UInt32 => uint.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Single => float.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Double => double.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Byte => byte.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_SByte => sbyte.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), System_Numerics_BigInteger => BigInteger.TryParse(value, out var result) ? result.Pass(out item) : value.Fail(out item), _ => conversionType.Fail(out item) } ;
}