VBA Conversion to Fractions

VBA Conversion to Fractions

Anonymous
Not applicable
612 Views
5 Replies
Message 1 of 6

VBA Conversion to Fractions

Anonymous
Not applicable
Yet another cry for help from my friends, I have an input box to get a specified distance that I will use to draw a part, later I want to use this to fill in the "cut list";
The probelm is the number is entered as decimal (62.12), I need to convert this to fraction (62-1/8, or "62-1/8")to put into the "cut list". I just can't find a way, FORMAT just don't get it- unless I missed something or need to create my own format, which I have no clue how to do. Isn't there an easy way to do this??
Thanks
Dave. K
0 Likes
613 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
KingCAD had this to say:

> Yet another cry for help from my friends, I have an input box to
> get a specified distance that I will use to draw a part, later I
> want to use this to fill in the "cut list";
> The probelm is the number is entered as decimal (62.12), I need to
> convert this to fraction (62-1/8, or "62-1/8")to put into the "cut
> list". I just can't find a way, FORMAT just don't get it- unless I
> missed something or need to create my own format, which I have no
> clue how to do. Isn't there an easy way to do this??

ThisDrawing.Utility.RealToString

--
http://www.acadx.com
0 Likes
Message 3 of 6

Anonymous
Not applicable
The following two function may help do what you want....

The first one will convert the decimal part of a number to a fraction. The
second function will let you round up or down to a nearest specified value
(i.e. .12 can be round to .125)..


Public Function Dec2Frac(ByVal dFraction As Double) As String

'Type: Public Function
'Name: Decimal to Fraction Converter
'Purpose: Converts a passed decimal to a fraction
'Limitations: Will not convert to mixed fractions
'Author: Unknown
'Arguments: dFraction is the decimal to convert
'Return Value: String representingthe fraction
'Useage: Dec2Frac(.125)
'Notes:

Dim df As Double
Dim lUpperPart As Long
Dim lLowerPart As Long

lUpperPart = 1
lLowerPart = 1

df = lUpperPart / lLowerPart

While (df <> dFraction)
If (df < dFraction) Then
lUpperPart = lUpperPart + 1
Else
lLowerPart = lLowerPart + 1
lUpperPart = dFraction * lLowerPart
End If
df = lUpperPart / lLowerPart
Wend

Dec2Frac = CStr(lUpperPart) & "/" & CStr(lLowerPart)

End Function

Public Function RoundToValue(ByVal nValue, ByVal nCeiling As Double, _
Optional RoundUp As Boolean = True) As Double

'Type: Public Function
'Name: Round to Value
'Purpose: Round a value up or down to the nearest specified ceiling value
'Limitations:
'Author: Veign - www.veign.com

Dim tmp As Long
Dim tmpVal
nValue = CDbl(nValue)

'Round up to a whole integer - Any decimal value will force a round to
'the next integer.
'i.e. 0.01 = 1 or 0.8 = 1

tmpVal = ((nValue / nCeiling) + (-0.5 + (RoundUp And 1)))
tmp = Fix(tmpVal)
tmpVal = CInt((tmpVal - tmp))
nValue = tmp + tmpVal

'Multiply by ceiling value to set RoundtoValue
RoundToValue = nValue * nCeiling

End Function

--
Veign
www.veign.com
Code Samples & Sample Projects
http://www.veign.com/information/application/info_app.html
Submit Your Best Code (you keep the rights)
http://www.veign.com/information/application/code_submit.html
---------
"KingCAD" wrote in message
news:f0da9a7.-1@WebX.maYIadrTaRb...
> Yet another cry for help from my friends, I have an input box to get a
specified distance that I will use to draw a part, later I want to use this
to fill in the "cut list";
> The probelm is the number is entered as decimal (62.12), I need to convert
this to fraction (62-1/8, or "62-1/8")to put into the "cut list". I just
can't find a way, FORMAT just don't get it- unless I missed something or
need to create my own format, which I have no clue how to do. Isn't there an
easy way to do this??
> Thanks
> Dave. K
>
Message 4 of 6

Anonymous
Not applicable
Okay, how would I use this?
This is the line I'm using - Set ObjText = ThisDrawing.ModelSpace.AddText(Format(DblWide(1), "0.00"), VarStartPnt, 0.65) ; When (DblWide(1)= 62.12 I print "62.12" I need to print "62-1/8" rounding to the nearest 1/16, so I don't end up with "62-1/326598" or whatever.
THANK YOU.
Dave. K
0 Likes
Message 5 of 6

Anonymous
Not applicable
KingCAD had this to say:

> Okay, how would I use this?

ThisDrawing.Utility.RealToString(62.12, acFractional, 3) returns 62
1/8. Be sure to read the online help for details.

--
http://www.acadx.com
0 Likes
Message 6 of 6

Anonymous
Not applicable
That's got it!!
Thank you very much!
Dave. K
0 Likes