I need to convert the contents of a Textbox (which is currency) to a datatype float. Would I convert to single?
txtPurchItemCorrectPrice.Text.Trim();
Strings can be converted to numbers by using the int() and float() methods.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With