Fraction to decimal  
Author Message
AndersBank





PostPosted: Visual Basic Language, Fraction to decimal Top

Hi! I have a list with prices of US Treasury bonds. They are quoted in a particular way that I would like to convert to decimal form. They are quoted like this eg: 99-02. Now this means that the price is 99 and some decimal. The two first numbers are the number of 32:s. Thus in this case the price is 99 and 2/32. However if there are three decimals eg 99-021 the last digit refers to the number of 124:s. Thus the price is 99 and 2/32 and 1/256. Sometimes There are only two decimals followed by a plus or minus sign eg 99-30+ or 99-10-. The plus/minus indicates that you should add/subtract 1/64.

Keep in mind that the non decimal numbers may be one, two or three digits. Thus, it could be eg. 6-02 87-22- or 105-21+. Is there any way that I can convert these price quotes into decimal form by creating a user defined function in vba I have trouble coming up with code that searches for the dividing sign between non decimal numbers and decimals numbers (ie “-“) and then work it from there. Any help very much appreciated! Thanks a lot in advance!



Visual Basic15  
 
 
Menthos





PostPosted: Visual Basic Language, Fraction to decimal Top

The way I'd tackle that would be to cast it to a string (if necessary) and then split out the component parts with simple string functions... use INSTR to find the position of the separator char.


 
 
Spidermans_DarkSide





PostPosted: Visual Basic Language, Fraction to decimal Top

Hi,

99-021 = 99 + 2/32 + 1/124 '      seems to me that this one makes the most

'sense as each part is almost double the previous part.

  Is it not 128 'ths

or 99-021 = 99 + 2/32 + 1/256 '     which is it please as your question isn't clear.

99-30+ would be 99 + 3/32 + 1/64 then

 

Regards S_DS

 



 
 
Ethan Pack





PostPosted: Visual Basic Language, Fraction to decimal Top

Private Function ConvertBondPrice(bondPrice as String) As Decimal
Dim priceElementArray as String()

' May want to validate bondPrice here to ensure correct formatting

priceElementArray = bondPrice.Split("-")

Dim fractionalElement as Decimal
fractionalElement = Integer.Parse(priceElementArray(1)) / 32

Dim outputValue as Decimal
outputValue = Decimal.Parse(priceElementArray(0)) + fractionalElement

Return outputValue
End Function

 
 
Spidermans_DarkSide





PostPosted: Visual Basic Language, Fraction to decimal Top

 

Hi,

I done this.>>

Just CUT & PASTE THE FUNCTION out of the following VB.Net CLASS code if you want to use them in VBA.

Public Class Bond

Private Cost As String

Public Sub New()

End Sub

Function ToDecimal(ByVal Price As String) As Decimal

Dim currPrice As String

currPrice = Price

Dim len As Integer = currPrice.Length

Dim finalCost As Decimal = 0

'Has price got 64's

If currPrice.Substring(len - 1, 1) = "+" Then

finalCost = finalCost + (1 / 64)

'currPrice = currPrice.Substring(0, len - 1)

finalCost = finalCost + CDec((currPrice.Substring(len - 3, 2)) / 32)

'Get Integer part.

Dim index2 As Integer = InStr(currPrice, " ") - 1

Dim mySub2 As String = currPrice.Substring(0, index2)

finalCost = CDec(mySub2) + finalCost

Return finalCost

ElseIf currPrice.Substring(len - 1, 1) = "-" Then

finalCost = finalCost - (1 / 64)

'currPrice = currPrice.Substring(0, len - 1)

finalCost = finalCost + CDec((currPrice.Substring(len - 3, 2)) / 32)

'Get Integer part.

Dim index3 As Integer = InStr(currPrice, " ") - 1

Dim mySub3 As String = currPrice.Substring(0, index3)

finalCost = CDec(mySub3) + finalCost

Return finalCost

End If

'Check for 3 final numbers.

Dim mySub As String

mySub = currPrice.Substring(len - 3, 1)

If IsNumeric(mySub) = True Then

'Number has three last digits.

finalCost = finalCost + CDec((currPrice.Substring(len - 3, 2)) / 32) + _

CDec((currPrice.Substring(len - 1, 1)) / 124)

Else

'Number only has two last digits.

finalCost = finalCost + CDec((currPrice.Substring(len - 2, 2)) / 32)

End If

'Get Integer part.

Dim index1 As Integer = InStr(currPrice, " ") - 1

Dim mySub1 As String = currPrice.Substring(0, index1)

finalCost = CDec(mySub1) + finalCost

Return finalCost

End Function

Public Property value() As String

Get

Return Cost

End Get

Set(ByVal value As String)

Cost = value

End Set

End Property

End Class

 

 

'  Then my FORM code is just.>> 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myBond As New Bond

myBond.value = TextBox1.Text

Dim myDec As Decimal

myDec = myBond.ToDecimal(myBond.value)

TextBox2.Text = CStr(myDec)

End Sub

 

  Instructions>> Type BOND price in any of these FORMATS into textbox1

99 17+

99 17-

99 17

Clicking button1 gives the following output to textbox2>>

99.546875

99.515625

99.53125

Is this what you are after

 

Regards,

S_DS

 

P.S. Would you need reverse conversion

 



 
 
AndersBank





PostPosted: Visual Basic Language, Fraction to decimal Top

Hi! No you are actually wrong here. If you are interested please look at top of page 10

http://www.newyorkfed.org/cfcbsweb/Treasuries_and_agencies.pdf

Also when you think about it, using 32:s 64:s and 256: makes most sense. 1/8/32 = 1/256.


 
 
AndersBank





PostPosted: Visual Basic Language, Fraction to decimal Top

Thank you very much! That was a very good answer! Thanks again!!!!!
 
 
AndersBank





PostPosted: Visual Basic Language, Fraction to decimal Top

Well reverse conversion would of course be an additional function that would be interesting. If you feel like helping me with this I would be most grateful. You have been most helpful so far!