Rounding to a specific number of decimals

Rounding to a specific number of decimals

Anonymous
Not applicable
413 Views
1 Reply
Message 1 of 2

Rounding to a specific number of decimals

Anonymous
Not applicable

Hey all,

Here is my code: 

Dim oSheetMetalCompDef As SheetMetalComponentDefinition
    Set oSheetMetalCompDef = oDoc.ComponentDefinition
    
    Dim thick As Double
    thick = oSheetMetalCompDef.Thickness.Value / 2.54
    thick = Round(thick, 3)
    thick = CStr(thick)
    
    If thick < 1 Then
        thick = Replace(thick, "0.", "")
    End If
    
    If thick = "25" Then
        thick = "250"
    ElseIf thick = "5" Then
        thick = "500"
    ElseIf thick = "75" Then
        thick = "750"
    ElseIf thick = "1" Then
        thick = "1.00"
    ElseIf thick = "1.5" Then
        thick = "1.50"
    ElseIf thick = "2" Then
        thick = "2.00"
    ElseIf thick = "2.5" Then
        thick = "2.50"
    End If

I would like for the sheet metal thicknesses to be rounded as they are above but I'm looking for an easier way to do it. For example I want thickness of 1/4 to return 250, 1/2 to return 500, 3/4 to return 750, 1 1/2 to return 1.50 and so on. Inventor seems to remove the trailing zeros when it gets the thickness so I am having difficulty adding them back in. Also, len(thick) returns 8 even when MsgBox thick returns "25", or "5", or any other thickness. Any help would be great.

 

Thanks,

Brendan Sullivan

0 Likes
Accepted solutions (1)
414 Views
1 Reply
Reply (1)
Message 2 of 2

nmunro
Collaborator
Collaborator
Accepted solution

There are a number of ways you could do this a bit easier. Here is a VBA sub that formats the value in the manner you are expecting.

 

Public Sub Round()

    Dim oDoc As PartDocument
    Set oDoc = ThisDocument
    
    Dim oSheetMetalCompDef As SheetMetalComponentDefinition
    Set oSheetMetalCompDef = oDoc.ComponentDefinition
    
    Dim thick As Double
    Dim thickAsInt As Integer
    Dim thickAsString As String
    
    thick = oSheetMetalCompDef.Thickness.Value / 2.54
    thickAsInt = CInt(Math.Round(thick, 3) * 1000)
    If (thickAsInt >= 1000) Then
      thickAsString = Format$(thickAsInt / 1000, "#.00")
    Else
      thickAsString = Format$(thickAsInt, "#")
    End If
    
    Debug.Print thickAsString
   
End Sub

 

 

        


https://c3mcad.com