I would very much appreciate if anyone could have a look at the following code and...........
A. tell me why it won't (when it used to) change the precision of mass to 2 decimal places when mass < 1.
B. help me to include the leading zero when mass < 1.
Ever since I inserted "0" before the Str on this line, which added the leading zero but removed the second decimal place I have had the issue with missing decimal.
This is all done in order to have the mass precision change automatically in my drawing template.
mass = iProperties.Mass iProperties.Value("Custom", "Mymass") = iProperties.Mass If mass < 1 Then iProperties.Value("Custom", "Mymass")= Str ( Round(iProperties.Mass ,2))+ " kg " If mass <= 10 Then iProperties.Value("Custom", "Mymass")= Str( Round(iProperties.Mass ,1))+ " kg " If mass > 10 Then iProperties.Value("Custom", "Mymass")= Str ( Round(iProperties.Mass ,0))+ " kg "
Any help will be most welcome.
Solved! Go to Solution.
Solved by MegaJerk. Go to Solution.
try this.
Dim mass As Decimal mass = iProperties.Mass Dim massString As String Select Case mass Case < 1 massString = CStr(Round(mass, 2, MidpointRounding.AwayFromZero))& " kg" Case 1 To 10 massString = CStr(Round(mass, 1, MidpointRounding.AwayFromZero))& " kg" Case > 10 massString = CStr(Round(mass, 0, MidpointRounding.AwayFromZero))& " kg" End Select iProperties.Value("Custom", "Mymass")= massString
Great stuff, that's excellent.
Thanks a lot.
Any ideas why my original stopped working?
It actually took me a moment to realize why your original code was failing, and it is the reason that I re-wrote your code into a Select Case statement. The reason you kept getting the wrong answer is due to your code ‘falling through’. Essentially, if you meet the first condition you present X < 1 Then by default you ALSO meet the condition for the next statement X <= 10.
Because the code is not nested, it easily deceives you into thinking that there is no problem with the conditional flow that is presented. Due to how us humans tend to read things, we get to that first line and think, “Oh… well I am most certainly not over 1, so nothing else should happen after this is done!” However, the compiler, realizing the next line is also true updates our value accordingly and moves on to provide you with an unexpected result!
I hope that this clears up the mystery!
That makes sense thanks.
I think possibly when it worked I had the first condition (<1) as the second condition which would allow this condition to be met. I re-ordered to tidy up a bit and obviously didn't realize why I had placed it second in the first place!
I hope that makes sense.
Thanks again though much appreciated.