An Int divided by an Int gives an Int...
You can find functions from Google, like :
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
An Int divided by an Int gives an Int...
You can find functions from Google, like :
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
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?
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)
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