Hello,
Can I use commas to sperate thousands and millions in ZPL with the FORMAT keyword?
For example, I want the number 123456789 to be formatted as 123,456,789.
Solved
Commas to separate thousands and millions in ZPL
Best answer by David Nguyen
This is not directly possible from the FORMAT keyword.
However, you can create a sub-macro that takes your number as an argument and format it accordingly. You can then call this sub-macro whenever it is needed.
I have drafted this sub-macro and a dummy macro to illustrate its function.
If you copy those to macro to your \Documents\Zemax\Macros folder and execute Comma_example.ZPL, this will give the following result:

Please note that you'd need to modify this macro if you want to account for numbers greater than 999,999,999.
Kind regards,
David
However, you can create a sub-macro that takes your number as an argument and format it accordingly. You can then call this sub-macro whenever it is needed.
I have drafted this sub-macro and a dummy macro to illustrate its function.
Dummy macro defining the number to be formatted
# Define a number a_number = 123456789 # Print the number PRINT "Input number = ", a_number PRINT "Calling formatting sub-macro..." PRINT "" PRINT "Formatted number = ", # Define the number as an input for the sub-macro CALLSETDBL 1, a_number # Call the formatting sub-macro CALLMACRO Comma_separation.ZPL
Sub-macro formatting the numbers with commas
# Retrieve input number
a_number = CALD(1)
# Change format to 3 digits integer
FORMAT 3 INT
# If input number is smaller than 1000 ...
IF (a_number > 999)
# and if input number is smaller than 1000000 ...
IF (a_number > 999999)
a_string$ = $STR(a_number)
a_string$ = $RIGHTSTRING(a_string$, 6)
# ... otherwise, we print the millions, add a coma
PRINT $STR(a_number/999999), ",",
# print the thousands, add another coma
PRINT $LEFTSTRING(a_string$, 3), ",",
# and then print the rest
PRINT $RIGHTSTRING(a_string$, 3)
ELSE
a_string$ = $STR(a_number)
# ... we first print the thousands, add a coma
PRINT $STR(a_number/1000), ",",
# and then print the rest
PRINT $RIGHTSTRING(a_string$, 3)
ENDIF
ELSE
# ... we just print that number
PRINT a_number
ENDIF
If you copy those to macro to your \Documents\Zemax\Macros folder and execute Comma_example.ZPL, this will give the following result:

Please note that you'd need to modify this macro if you want to account for numbers greater than 999,999,999.
Kind regards,
David
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.