Convert string into double

Convert string into double

18154093
Contributor Contributor
511 Views
2 Replies
Message 1 of 3

Convert string into double

18154093
Contributor
Contributor

Hi guys

I need convert string into double

ex: 1/12 => 0.8333

 

0 Likes
512 Views
2 Replies
Replies (2)
Message 2 of 3

g.georgiades
Advocate
Advocate

Hi,

 

If you don't have a fraction symbol - you can use double.Parse

Double.Parse(".523")
Double.Parse("-0.523")

 

For fractional formats, I found two options:

 

You can have a look at this post where inventor UOM is used to convert the value. You may have to experiment with the units.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/convert-fraction-to-decimal/m-p/9751... 

 

 

For a purely code solution see this stackoverflow post. I have converted the c# to vb.net/iLogic code for you

https://stackoverflow.com/questions/13903621/convert-a-text-fraction-to-a-decimal 

Private Function FractionToDouble(ByVal fraction As String) As Double
        Dim result As Double

        If Double.TryParse(fraction, result) Then
            Return result
        End If

        Dim split = fraction.Split(New Char() {" "c, "/"c})

        If split.Length = 2 OrElse split.Length = 3 Then
            Dim a, b As Integer

            If Integer.TryParse(split(0), a) AndAlso Integer.TryParse(split(1), b) Then
                If split.Length = 2 Then
                    Return a / b
                End If

                Dim c As Integer

                If Integer.TryParse(split(2), c) Then
                    Return a + b / c
                End If
            End If
        End If

        Throw New FormatException("Not a valid fraction.")
End Function

 

 

Message 3 of 3

WCrihfield
Mentor
Mentor

Hi @18154093.  This is what I had in mind for your challenge.  Inventor's built-in UnitsOfMeasure object has many useful tools for things like this.  Also, I just assumed you were working with inches, but if that is not the case, you can change those aspects within the code.  The database units for length are centimeters, and it seems like very few people actually use that as their default document units.

Dim oStVal As String = "1/12"
Dim UOM As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
Dim oDblVal As Double = UOM.GetValueFromExpression(oStVal, "in")
oDblVal = UOM.ConvertUnits(oDblVal, "cm", "in")
MsgBox("oDblVal = " & oDblVal,,"")

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)