Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string to a float data type

I need to convert the contents of a Textbox (which is currency) to a datatype float. Would I convert to single?

txtPurchItemCorrectPrice.Text.Trim();

like image 497
user279521 Avatar asked Jul 01 '10 13:07

user279521


People also ask

Can you convert string to int or float?

Strings can be converted to numbers by using the int() and float() methods.

Which function is used to convert string data to float data in Python?

float() function is the most common way to convert the string to the float value. The float function takes the parameter y, which is a string, and would then convert the string to a float value and return its float value.

How do you convert string to float in ML?

You can use the float() function to convert any data type into a floating-point number. This method only accepts one parameter. If you do not pass any argument, then the method returns 0.0. If the input string contains an argument outside the range of floating-point value, OverflowError will be generated.


1 Answers

If you're dealing with currency then I would use double at the very least, if not decimal. That said you want:

double value = double.Parse(txtPurchItemCorrectPrice.Text.Trim());

If you're not sure whether it will be a number or not:

double value;
bool isOK = double.TryParse(txtPurchItemCorrectPrice.Text.Trim(), out value);
like image 200
ChrisF Avatar answered Oct 27 '22 09:10

ChrisF