568-How to Round Positive Floating-Point Numbers in Netlinx
Technical Support Guide
Table of Contents
Brand:
- AMX
Models:
- Netlinx
Question:
How do I Round Positive Floating-Point Numbers in Netlinx?
Answer:
Here's a simple function to round any positive floating-point number to the nearest whole number.
DEFINE_FUNCTION Integer Round (Float fInput)
{
Return TYPE_CAST (fInput + 0.5);
}It works by taking advantage of the characteristics of NetLinx 'TYPE_CAST' operator and integers. When a float is type cast into an integer, the decimal portion of the original number is dropped. By first adding 0.5 to the number, this function ensures that all numbers get rounded to the nearest whole. (1/2 gets rounded up, as is customary.)
Example:
fTest = 2.63
nVar = Round (fTest) // nVar will be set to 3
Heading 2: [eg. Instructions, Procedure, Description]
- [Use numbered lists]
- [for instructions or]
- [processes]
Heading 3: [eg. Final Thoughts, Pro-Tips]
[Add any additional info here]