Convert string to floating point data type

I need to convert the contents of a text field (which is a currency) to a data type float. Will I convert to single?

txtPurchItemCorrectPrice.Text.Trim ();

+5
source share
4 answers

If you are dealing with currency, I would use double, at least, if not decimal. You said:

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

If you are not sure whether this number will be or not:

double value;
bool isOK = double.TryParse(txtPurchItemCorrectPrice.Text.Trim(), out value);
+9
source

Do you mean type C # float?

float f = float.Parse(text);

Or...

float value;
if (float.TryParse(text, out value))
{
     // Yay!
}
else
{
     // Boo! Parse failed...
}

Please note that the code above will use the current culture. You can specify a different culture, for example

...
if (float.TryParse(text, out value, NumberStyles.Float,
                   CultureInfo.InvariantCulture))
...

EDIT: ​​ , double.

, float/double; , . decimal (#) NUMBER (SQL).

+4
float.TryParse(…)

This avoids throwing an exception due to incorrect input.

0
source

Dim x As Double = Convert.ToDouble (txtPurchItemCorrectPrice.Text.Trim ())

0
source

All Articles