ArcObjects Library Reference  (System)

# FractionFormat Example

1. Copy the code below.
2. In ArcMap, select Macros... from Tools | Macros (or press Alt-F8).
3. Enter a new macro name and click the Create button.
4. Paste the copied code into the code pane.
5. Press F5 to run the macro.
```[Visual Basic 6.0]Sub FractionFormatExample()
' Create a FractionFormat object
Dim pFractionFormat As IFractionFormat
Set pFractionFormat = New FractionFormat

' The ValueToString & StringToValue methods are in the
' INumberFormat interface.  QI for INumberFormat to
' use them.
Dim pNumberFormat As INumberFormat
Set pNumberFormat = pFractionFormat

' Set FractionFormat options
With pFractionFormat
.FractionOption = esriSpecifyFractionDigits
.FractionFactor = 3   'Calculate numerator & denominator to 3 digits
End With

' Define some sample fractions
Dim colTestData As New Collection
With colTestData
.Add "893/1234"   '<-- This is calculated as 474/655 (it is the
End With            '    closest 3 digit fraction to this value). To
'    calculate it more precisely, set FractionFactor
'    to a higher value. The caveat here is it
'    takes a lot of processing time to figure out
'    the fraction for higher FractionFactor's.

' Use StringToValue to decode the given fractions to decimal
' values and ValueToString to reformat them back into fractions
Dim vFraction As Variant, sV2S As String, dS2V As Double
For Each vFraction In colTestData
dS2V = pNumberFormat.StringToValue(vFraction)
sV2S = pNumberFormat.ValueToString(dS2V)
MsgBox "StringToValue('" & vFraction & "') = " & dS2V & vbNewLine & _
"ValueToString(" & dS2V & ") = '" & sV2S & "'", , _
"FractionOption = esriSpecifyFractionDigits"
Next

' If you know what the denominator is, set
' FractionOption to esriSpecifyFractionDenominator
Set colTestData = New Collection
With pFractionFormat
.FractionOption = esriSpecifyFractionDenominator
.FractionFactor = 8   'Now FractionFactor specifies the denominator