How to handle a case when Convert.ToSingle operates on a non numeric value.

brgdotnet
brgdotnet used Ask the Experts™
on
I am iterating through a text file using VB.net. Some of the numeric values are stored as strings. I would like to use the VB.net command Convert.ToSingle to convert the string input into a decimal(Or floating point number). How shall I handle the case when the string value is not numeric, because an exception will be thrown? I wish there was a command that could in effect look ahead to determine if the value is not numeric.



Dim num As Single
num - Convert.ToString(inString) ' Exception thrown here if the inString value is not numeric
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead
Commented:
How shall I handle the case when the string value is not numeric, because an exception will be thrown?

well, so you got to do with a try ... catch method, if there's any exception, then we know the conversion got issue.

try-catch (C# Reference)
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
Fernando SotoRetired
Distinguished Expert 2017
Commented:
Hi brgdotnet;

You could use the Single.TryParse method [here]. Please see the link for the documentation and example. If you need more direction please post a comment here.

Fernando
Imports System.Globalization
Imports System.Text.RegularExpressions

    Public Shared Function TryGetDouble(s As String, ByRef value As Double) As Boolean
        Dim x = Regex.Replace(s, "[^0-9\,\.\-]", "").Replace(",", ".").Trim
        Return Double.TryParse(x, NumberStyles.AllowDecimalPoint Or NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, value)
    End Function
    Public Shared Function GetDouble(s As String, Optional defaultValue As Double = 0) As Double
        Dim v As Double
        If TryGetDouble(s, v) Then Return v Else Return defaultValue
    End Function

    Public Shared Function TryGetSingle(s As String, ByRef value As Single) As Boolean
        Dim x = Regex.Replace(s, "[^0-9\,\.\-]", "").Replace(",", ".").Trim
        Return Single.TryParse(x, NumberStyles.AllowDecimalPoint Or NumberStyles.AllowLeadingSign,  CultureInfo.InvariantCulture, value)
    End Function
    Public Shared Function GetSingle(s As String, Optional defaultValue As Single = 0) As Single
        Dim v As Single
        If TryGetSingle(s, v) Then Return v Else Return defaultValue
    End Function

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

brgdotnetcontractor

Author

Commented:
Thanks everyone, so awesome! Hey one more thing. I converted the number 6000 from a string to a Single data type, but it comes out as 6000

So what I really need is for it to convert with a decimal at the end. So it should come out to be 6000.00 or 6000.0

So how can that be achieved?

Below is my code.
Dim strRepOfNumber As String
strRepOfNumber = "6000"
Dim myRealNumber As Single
If (Single.TryParse(strRepOfNumber, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, myRealNumber) Then
 ' Note String of 6000 is converted to 6000 but I need a decimal at the end of the number. How to do that?
End If
Ryan ChongSoftware Team Lead
Commented:
Note String of 6000 is converted to 6000 but I need a decimal at the end of the number

for me, it's a formatting issue, since the single value of 6000 and 6000.0 are exactly the same.
Format your single when output it:
Dim s = myRealNumber.ToString("#0.00")

Open in new window

Retired
Distinguished Expert 2017
Commented:
Hi brgdotnet;

You can use the, ToString(“F”), for example
Dim result = MySingleValue.ToString(“F”)

Open in new window

Should result with 2 decimal digits. If you need 3 decimal digits then change “F” to “F3” and so forth.

Fernando

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial