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!