Anytime Help Center

AMX How to Round Positive Floating-Point Numbers in Netlinx

Rating

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

Downloads

Product

 

Topic

Programming

Related Articles

Last modified at 3/30/2023 11:43 AM by PRO Knowledge Base
Top