Problem with a fraction 1/2

Les 281 Reputation points
2021-03-18T18:32:13.253+00:00

This is probably a crazy question. I have a string value of 1/2 and want to use the value of it in an equation however every time I look at its value that should be .5 it is telling me its a 1 as if it is acting like it is being CINT???

Here is my code snippet

            Dim v As String = "1/2"
            Dim vv As Double
            vv = CSng(Val(v))

I do not care if it is a single or double value just as long as it is the right value, what am I missing and I don't even know how long I have been doing this which concerns me.

Thanks in advance and if you can explain how to do this properly I would appreciate.

Thanks

Les

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-03-18T19:14:49.28+00:00

    An Int divided by an Int gives an Int...

    You can find functions from Google, like :

    Fraction To Decimal


4 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-03-18T20:57:13.38+00:00

    Hello,

    Add the following code module to your project.

    Public Module Utilities
        <Runtime.CompilerServices.Extension>
        Public Function FractionToDouble(ByVal fraction As String) As Double
            Dim result As Double = 0
    
            If Double.TryParse(fraction, result) Then
                Return result
            End If
    
            Dim split() As String = fraction.Split(New Char() {" "c, "/"c})
    
            If split.Length <> 2 AndAlso split.Length <> 3 Then
                Throw New FormatException("Not a valid fraction.")
            End If
    
            Dim item1 As Integer
            Dim item2 As Integer
    
            If Integer.TryParse(split(0), item1) AndAlso Integer.TryParse(split(1), item2) Then
    
                If split.Length = 2 Then
                    Return CDbl(item1) / item2
                End If
    
    
                Dim item As System.Int32
                If Integer.TryParse(split(2), item) Then
                    Return item1 + CDbl(item2) / item
                End If
            End If
    
            Throw New FormatException("Not a valid fraction.")
    
        End Function
    
    End Module
    

    Test

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim values() As String = {"3/4", "1/4", "1/2", "2/32"}
    
            For Each value In values
                Debug.WriteLine($"{value} -> {value.FractionToDouble()}")
            Next
    
        End Sub
    
    End Class
    

    Results in the for/each are of type Double resulting with

    3/4 -> 0.75
    1/4 -> 0.25
    1/2 -> 0.5
    2/32 -> 0.0625
    
    1 person found this answer helpful.

  2. Viorel 122.6K Reputation points
    2021-03-18T19:23:39.893+00:00

    The standard parsing and conversion functions can parse strings like “1” or “0.5”, but it cannot parse expressions like “1/2” or “sin(3)”. You probably need some special expression parser, or write your own (esp. if the expressions are simple). What kind of expressions do you want to parse?


  3. Castorix31 90,686 Reputation points
    2021-03-20T07:04:09.617+00:00

    If you want short code, you can also use the Microsoft ScriptControl to parse the expression =>

    Dim scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"))
    Dim objScriptControl As Object = Activator.CreateInstance(scriptType)
    objScriptControl.Language = "vbscript"
    Dim sExpression As String = "1/2"
    Dim objResult As Double = objScriptControl.Eval(sExpression)
    
    0 comments No comments

  4. Dewayne Basnett 1,381 Reputation points
    2021-03-22T15:56:36.03+00:00

    Along the same lines as what Karen did but using Nullable of Decimal.

    Add this extension.

    Public Module NumExts
        ''' <summary>
        ''' convert fraction string to decimal
        ''' </summary>
        ''' <param name="fraction">fraction string</param>
        ''' <returns>nullable of Decimal. no value is error</returns>
        ''' <remarks></remarks>
        <Runtime.CompilerServices.Extension>
        Public Function FractionToDecimal(fraction As String) As Decimal?
            Dim rv As Decimal? 'no value
    
            If fraction.Contains("/"c) Then
    
                Dim terms() As String = fraction.Split(New Char() {" "c, "/"c},
                                                        StringSplitOptions.RemoveEmptyEntries)
    
                If terms.Length = 2 Then
                    Dim dividend As Decimal
                    Dim divisor As Decimal
                    If Decimal.TryParse(terms(0), dividend) AndAlso
                          Decimal.TryParse(terms(1), divisor) Then
                        rv = dividend / divisor
                    End If
                End If
            End If
            Return rv
        End Function
    
    End Module
    

    Usage test

            Dim fracs As String
            Dim frac As Decimal? 'nullable
    
            fracs = "22/7"
            frac = fracs.FractionToDecimal
            If Not frac.HasValue Then
                Stop 'not a fraction
            End If
    
            fracs = "a / 6"
            frac = fracs.FractionToDecimal
            If Not frac.HasValue Then
                Stop 'not a fraction
            End If
    
            fracs = "4 / 9"
            frac = fracs.FractionToDecimal
            If Not frac.HasValue Then
                Stop 'not a fraction
            End If
    
            fracs = "8 / 4"
            frac = fracs.FractionToDecimal
            If Not frac.HasValue Then
                Stop 'not a fraction
            End If
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.