Rounding numeric values when creating a ZPL macro

  • 9 April 2022
  • 2 replies

Good afternoon.

I have a question about working with numeric variables when writing an ZPL macro. Is it possible to round the values assigned to variables with the required accuracy? I'm looking for functions similar to (FLOOR.MATH) and (CEILING.MATH) in Microsoft Excel. 

I understand that i can create a loop that allows me to sequentially compare the value of interest with i and i+1 value. I would like to create an ZPLM operand using rounding, so it seems to me that this way will slow down the optimization, especially with a high degree of rounding accuracy.

As an example, we can discuss the case where the value 24.432 needs to be rounded up to 24.0 or 24.5.



Best answer by MichaelH 11 April 2022, 19:40

View original

2 replies

Userlevel 5
Badge +2

Hi Devorse,

Rounding quite different than FLOOR.MATH/CEILING.MATH, especially when you start to consider the significance and mode properties of the Excel functions.  To round a value to a specific number of digits, you can simply multiply it by 10^n (where n is the number of digits), add/subtract 0.5, take the INTE() value and then divide by 10^n:

! val = input value

! n = number of digits

! res = result value

IF val > 0 

    res = INTE(val * POWR(10, n) + 0.5 * SIGN(val)) / POWR(10, n)


    res = INTE(val * POWR(10, n) - 0.5 * SIGN(val)) / POWR(10, n)


For the value of 24.432, this will never be rounded up to 24.5 though because the 100’s place is not greater than 5.  However, if you use 24.452, then you can get 24.0 if n = 0 and 24.5 if n = 1.  This gives the same values as the ROUND function.

Unless you have a very specific need, I’m not sure you need to implement the exact FLOOR.MATH/CEILING.MATH algorithms, especially seeing how these can often be unintuitive.  For example, FLOOR.MATH(-10, 4) gives -12 and FLOOR.MATH(-7.35, 0.2) gives -7.4.  If you need these exact algorithms, you will need to create a SUB routine which will calculate the modulus of the number and the significance and then calculate the exact value.


Hello Michael.
Thanks for the solution you suggested. I will also leave here my IF-THEN algorithm, which allows you to force rounding up or down with the required accuracy.
We are taught that when designing an imaging system, there are some guidelines for the element mounting allowance, component diameter, and minimum edge thickness to account for chamfer. I also know that there is an option in optical studio to add some % of the light diameter of a component as a mounting allowance, however the guidelines I try to follow are built on a slightly different principle.
Now I have the ability to control the minimum edge thickness of the part, taking into account the allowance for fastening and taking into account rounding to the normal range of values.