Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

formating text strings

Status
Not open for further replies.

rwbaker

Computer
Mar 27, 2004
37
I would like to know how i can format a text string like
3.877 to be 3 7/8

looking to round to the nearest #/16

thanks in advance
 
Replies continue below

Recommended for you

Can you use a dimension and format the dimension style?
 
Thanks for reply

What i have is a master drawing that i have created.
The drawing is not to scale.

I went in and explode all the dimensions
then i created attributes for the text

so now i have attributes for all the dimensions that i can replace when values that i calc in my program

the drawing is not to scale

the part that i am missing now is being able to round the dimension from decimals to fractions...then all would be done.

is there nothing in VBA that will allow rounding of decimals to fractions ?
 
VBA is a full-featured programming language. Multiply the decimal portion of your real number by 16 and take the integer portion of the result (or round it if you prefer). Formatting is really simple.

David
 
You can access the numbers in the attributes, they are stored as a string in the entity data. The lisp expression:
(rtos (atof NumberAsString)4 4))

will return a string of the number, rounded to the nearest 1/16.
 
For whatever reason I am having trouble figuring out how to
round numbers from a decmial format to fractions

Are there any vba scripts that will do this ?

I have several programs that i need to do this with.

guess i am brain dead on this one.....

 
Here is one I found (I did not check it)...

Function Dec2Fract(X As Single) As String
'------------------------------------------------------------------------------
' Returns a string of a number rounded to a whole and
'fraction in 16ths, 32nds, etc.
' Handles number > 1 and less than 0
' Use as follows:
' Label1.Caption = Dec2Fract(Val(Text1.Text))
' or Label1.Caption = Dec2Fract(Val(Label1.Caption)
'------------------------------------------------------------------------------
Dim F As String, Y As Single, Num As Integer, Den As Integer

Den = 16 'Denominator: can be set to 8, 16, 32, 64 etc

If X = 0 Then
Dec2Fract = "0"
Exit Function
Else

Y = Abs(X)
If Y > 1 Then Y = Y - Int(Y) ' get fractional part
Num = CInt(Den * Y)

If Num = Den Then
F = "1"
ElseIf Num = 0 Then
If Abs(X) < 1 Then F = "0" Else F = ""
Else
Do Until Num Mod 2 <> 0
Num = Num / 2
Den = Den / 2
Loop
F = LTrim$(Str$(Num)) + "/" + LTrim$(Str$(Den))
End If

If Abs(X) > 1 Then
If F <> "1" Then
F = Trim$(Str$(Fix(X))) + " " + F
Else
F = Trim$(Str$(CInt(X)))
End If
End If

If X < 0 And X > -1 Then F = "-" + F

Dec2Fract = F
End If
End Function

"Everybody is ignorant, only on different subjects." — Will Rogers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor