[RESOLVED] [2005] Conversion from string to type Double
Is it possible to convert the String from the text box to type Double?
Code:
price = CType(txtPrice.Text, Double)
This code does'nt work when the txtPrice.Text is empty. Sometimes I need to leave this txtPrice.Text as blank. If it is not possible to leave the said textbox as blank just let me know!
Thanks in Advance!
Re: [2005] Conversion from string to type Double
It's possible to convert a valid string representation of a number to a Double but if the string doesn't represent a number then what conversion could be made? If there's a chance that the string may be invalid then use the TryParse method the type you're converting to:
vb Code:
Dim price As Decimal
If Decimal.TryParse(txtPrice.Text, price) Then
'The conversion was successful and the "price" variable contains the number.
Else
'The conversion failed.
End If
Note that I used Decimal instead of Double. It will work with Double too but it is generally preferable to use Decimal for currency values as there is no risk of round-off errors.
Re: [RESOLVED] [2005] Conversion from string to type Double