iLogic not making sense - Math.Round

iLogic not making sense - Math.Round

CDuPlessisTQEZQ
Enthusiast Enthusiast
190 Views
2 Replies
Message 1 of 3

iLogic not making sense - Math.Round

CDuPlessisTQEZQ
Enthusiast
Enthusiast

Good day Autodesk Community.

 

I'm having a bit of a problem here regarding Inventor's iLogic and the Math.Round function.

I don't know if I'm just not seeing something or what but all I wish to do is round a number to the 1ste decimal and assign it to iProperties.

Now, it's been working well but now something is strange.

The cut size is 1682.650mm. When I use this code:

Math.Round(extents_width, 1)

 - it makes the cut size: 1682.6mm, and not 1682.7mm.

Rounding the .650mm to 1 decimal should make it .7mm. 

Why is mine making the number .6mm?

 

Attached is an image aswell. Using 2023 Inventor.

 

Here's my code:

extents_length = Math.Round(SheetMetal.FlatExtentsLength, 2)
extents_width = Math.Round(SheetMetal.FlatExtentsWidth, 2)

If extents_length >= extents_width Then
	iProperties.Value("Project", "Description") = Math.Round(extents_width, 1) & " X " & Thickness - 1.5 & "/" & Thickness & " THK PLT X " & Math.Round(extents_length, 1) & " LG"
Else
	iProperties.Value("Project", "Description") = Math.Round(extents_length, 1) & " X " & Thickness  - 1.5 & "/" & Thickness & " THK PLT X " & Math.Round(extents_width, 1) & " LG"
End If

 

0 Likes
Accepted solutions (1)
191 Views
2 Replies
Replies (2)
Message 2 of 3

ryan.rittenhouse
Advocate
Advocate
Accepted solution

You've hit one of my favorite (rage inducing) VB quirks. By default, the Round operation uses the ToEven rounding method, meaning when it hits a 5, it rounds towards the closest even number. I think this is common practice in financial operations, but is wrong for our purposes. You can fix this by specifying the round to and using AwayFromZero (example below). That should get you the result you are looking for.

 

Round(extents_width, 1, MidpointRounding.AwayFromZero)

 

 

If this solved your problem, or answered your question, please click Accept Solution.
Message 3 of 3

CDuPlessisTQEZQ
Enthusiast
Enthusiast

Thank you Ryan.

I appreciate your help.

Works perfect now and it round to the .7mm as intended.

0 Likes