converting string fractions to double or decimals
Author Message
converting string fractions to double or decimals

Hi,
Novice in VB.Net.
I would really appreciate if somebody can tell me how to
code to convert strings like "3/4" or "2 7/8" to their
corresponding decimal values - 0.75 or 2.875 -
Thank you in advance,
JoJo

Tue, 30 Aug 2005 07:04:25 GMT
converting string fractions to double or decimals
Hi,
Can you try this..

string str = "3";
double bdl;
bdl = double.Parse(str);

regards,
Prasanna Kakanoor

Quote:
>-----Original Message-----
>Hi,
>Novice in VB.Net.
>I would really appreciate if somebody can tell me how to
>code to convert strings like "3/4" or "2 7/8" to their
>corresponding decimal values - 0.75 or 2.875 -
>Thank you in advance,
>JoJo

>.

Tue, 30 Aug 2005 12:42:33 GMT
converting string fractions to double or decimals
Hello Prasana,

Quote:
>  Can you try this..

>         string str = "3";
>         double bdl;
>         bdl = double.Parse(str);

... but this doesn't work with fractions.

Regards,
Herfried K. Wagner

Tue, 30 Aug 2005 15:46:22 GMT
converting string fractions to double or decimals
Hi JoJo,
I dont know any direct way to solve this problem.
Try this. It should work.
Greetings
Daniel

Private Function Converter(ByVal stDiv As String)
Dim i, k As Integer, sNum, operator As String
Dim Result, nNum1, nNum2 As Double
For i = 0 To stDiv.Length - 1
If IsNumeric(stDiv.Substring(i, 1)) Then
sNum = sNum + stDiv.Substring(i, 1)
nNum1 = CInt(sNum)
operator = stDiv.Substring(i + 1, 1)
k = i + 1
Else
Exit For
End If
Next
sNum = ""
For i = k To stDiv.Length - 1
If IsNumeric(stDiv.Substring(i, 1)) Then
sNum = sNum + stDiv.Substring(i, 1)
nNum2 = CInt(sNum)
End If
Next
Select Case operator
Case "/"
Return nNum1 / nNum2
Case "+"
Return nNum1 + nNum2
Case "-"
Return nNum1 - nNum2
Case "*"
Return nNum1 * nNum2
Case Else
MsgBox("Your input is no valid expession!",
MsgBoxStyle.Critical, "Error")
End Select
End Function

Quote:
> Hi,
> Novice in VB.Net.
> I would really appreciate if somebody can tell me how to
> code to convert strings like "3/4" or "2 7/8" to their
> corresponding decimal values - 0.75 or 2.875 -
> Thank you in advance,
> JoJo

Wed, 31 Aug 2005 19:28:59 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages