Skip to main content
Solved

Commas to separate thousands and millions in ZPL

  • October 15, 2019
  • 4 replies
  • 282 views

David Nguyen
Zemax Staff
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.

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.

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
View original
Did this topic help you find an answer to your question?

4 replies

David Nguyen
Zemax Staff
  • Author
  • Zemax Staff
  • 23 replies
  • Answer
  • October 15, 2019
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.

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

Hey David,

I think there might be a way to do it with just the FORMAT keyword. We support this structure:
 
FORMAT "C_format_string" [LIT]
The optional keyword LIT (for literal) indicates the value should be printed according to the “C” language format specifier. I'll bet (without proof, but that's why it's betting) that C supports this kind of format directly.

Best,

- Mark

Actually, I can't find a C format spec for this...I'm really surprised!


David Nguyen
Zemax Staff
  • Author
  • Zemax Staff
  • 23 replies
  • October 16, 2019
Hi Mark,


I did spend a little bit of time searching for the C syntax that would give me this number formatting. However, to my surprise as well, it was not easy to figure out.

I ended up writting this sub-macro, but I'm happy to change it if someone finds a better solution!

Kind regards,


- Davd

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings