I'm still fighting hard with the usage of VB in forms for IM. Current topic: "round" a value, e.g. from 0.6666667 to 0.67.
Errormessage: Error:"round" is not declared
The images show code without and with "round" command.
What to do?
Solved! Go to Solution.
I'm still fighting hard with the usage of VB in forms for IM. Current topic: "round" a value, e.g. from 0.6666667 to 0.67.
Errormessage: Error:"round" is not declared
The images show code without and with "round" command.
What to do?
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
I assume you do IM API programming with VB.NET (Visual Studio 2015/7/9). The issue really has nothing to do with IM, or IM Form, rather than basic understanding data types used for programming.
Expression 2/3 (2 to be divided by 3), as you know, would result in a number with infinite decimals (it should be a "Single" or "Double" type in VB.NET.
So, firstly you SHOULD NOT declare a "String" type to hold this value. Unfortunately VB.NET, due to its historical reason, tries to do more than it should (to make beginner programmer to feel programming is easy!) and automatically convert the result of 2/3 into a String value, "0.6666666", not 0.666666....(a Double value). If you use other strong typed language, your code (result=2/3) would not pass syntax check.
Now understanding that 2/3 will result in a Double value, which doe not exactly equal 0.666, nor 0.66666, then what is its real value? In programming, its ultimate value is determined by how much precision the computer's memory is designed to hold, thus the Single or Double data type.
In VB.NET, the result of expression 2/3 is considered as Double. Thus, you code should be
Dim result As Double = 2/3
And the variable "result" will hold a real number of 0.666666...... with as many as decimals a Double type could hold.
When you say "rounding" there are 2 meanings:
1. for business requirement. You may want to for some reason to actually round the number itself up/down, say to be 0.667000000....../0.666000000.....In this case, the rounded number is still a Double with still that many decimals that can be held by Double type, the the value itself has CHANGED by the rounding.
2. For readability/presentation purpose. In most cases, there is no need to show a number with too many decimals to make sense. It is simple in VB.NET/C# to simply do this:
Dim result As Double = 2.0/3.0
MsgBox (result.ToString("0.0000"), ... , ... )
In this case, the number is shown to user in a meaningful way (with the desired decimals), but the real value of the number is not changed.
This is the same way how AutoCAD presents geometries of entities. For example, you add a point in AutoCAD at
x=1.333333334567, y=2.333345678, AutoCAD still store the point as 2 Double number precisely as entered. When you set AutoCAD's unit precision to 0.000, you would be presented this point in AutoCAD as x=1.333, y=2.333. But if you set AutoCAD's unit precision to 0.000000, the point then is presented as x=1.333333, y=2.333345.
Therefore, when you say "rounding", you need to know what do you want to do.
If you really want to round the number (i.e. change its real value because of rounding up/down, then you do this:
Dim result As Double = 2.0/3.0
'' round to 3 decimals, The number becomes 0.6670000000..... (as many 0 as Double type can hold)
result = Math.Round(result, 3)
MsgBox(result.ToString("0.000000"), ...) '' Show as "0.667000"
MsgBox(result.ToString("0.00"), ...) '' Show as "0.67"
MsgBox(result.ToString("0.000"), ...) '' Show as "0.667"
HTH
Norman Yuan
I assume you do IM API programming with VB.NET (Visual Studio 2015/7/9). The issue really has nothing to do with IM, or IM Form, rather than basic understanding data types used for programming.
Expression 2/3 (2 to be divided by 3), as you know, would result in a number with infinite decimals (it should be a "Single" or "Double" type in VB.NET.
So, firstly you SHOULD NOT declare a "String" type to hold this value. Unfortunately VB.NET, due to its historical reason, tries to do more than it should (to make beginner programmer to feel programming is easy!) and automatically convert the result of 2/3 into a String value, "0.6666666", not 0.666666....(a Double value). If you use other strong typed language, your code (result=2/3) would not pass syntax check.
Now understanding that 2/3 will result in a Double value, which doe not exactly equal 0.666, nor 0.66666, then what is its real value? In programming, its ultimate value is determined by how much precision the computer's memory is designed to hold, thus the Single or Double data type.
In VB.NET, the result of expression 2/3 is considered as Double. Thus, you code should be
Dim result As Double = 2/3
And the variable "result" will hold a real number of 0.666666...... with as many as decimals a Double type could hold.
When you say "rounding" there are 2 meanings:
1. for business requirement. You may want to for some reason to actually round the number itself up/down, say to be 0.667000000....../0.666000000.....In this case, the rounded number is still a Double with still that many decimals that can be held by Double type, the the value itself has CHANGED by the rounding.
2. For readability/presentation purpose. In most cases, there is no need to show a number with too many decimals to make sense. It is simple in VB.NET/C# to simply do this:
Dim result As Double = 2.0/3.0
MsgBox (result.ToString("0.0000"), ... , ... )
In this case, the number is shown to user in a meaningful way (with the desired decimals), but the real value of the number is not changed.
This is the same way how AutoCAD presents geometries of entities. For example, you add a point in AutoCAD at
x=1.333333334567, y=2.333345678, AutoCAD still store the point as 2 Double number precisely as entered. When you set AutoCAD's unit precision to 0.000, you would be presented this point in AutoCAD as x=1.333, y=2.333. But if you set AutoCAD's unit precision to 0.000000, the point then is presented as x=1.333333, y=2.333345.
Therefore, when you say "rounding", you need to know what do you want to do.
If you really want to round the number (i.e. change its real value because of rounding up/down, then you do this:
Dim result As Double = 2.0/3.0
'' round to 3 decimals, The number becomes 0.6670000000..... (as many 0 as Double type can hold)
result = Math.Round(result, 3)
MsgBox(result.ToString("0.000000"), ...) '' Show as "0.667000"
MsgBox(result.ToString("0.00"), ...) '' Show as "0.67"
MsgBox(result.ToString("0.000"), ...) '' Show as "0.667"
HTH
Norman Yuan
Hi @norman.yuan
thanks a lot for your detailed reply.
Yes, the basic issue is my very low knowledge of VB and backgrund; this is why I'm searching for information about the special implementation here in the forms - e.g. the difference between
Me.Application.MessageBox("Hey Ho") versus MsgBox("Hey Ho")
But nevertheless, thanks to your hints I could make some "revolutionary" progress, and I want to add the screenshots for other great coders like me 🙄
Hi @norman.yuan
thanks a lot for your detailed reply.
Yes, the basic issue is my very low knowledge of VB and backgrund; this is why I'm searching for information about the special implementation here in the forms - e.g. the difference between
Me.Application.MessageBox("Hey Ho") versus MsgBox("Hey Ho")
But nevertheless, thanks to your hints I could make some "revolutionary" progress, and I want to add the screenshots for other great coders like me 🙄
Can't find what you're looking for? Ask the community or share your knowledge.