Announcement

Announcement Module
Collapse
No announcement yet.

Centering text within a field

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Centering text within a field

    Linda, Here's something I had published in Midrange Computing back in Sep 1999. If you have any questions, please let me know. Center Text Pronto! Looking for a quick way to center text in RPG IV? Try the technique I put together in Figure 2! It’s fast, simple, and, best of all, doesn’t take a lot of lines of code! — Michael C. Schlemme MSchlemme@Banta.com[*][*] Field to contain centered data (pick one definition)[*] D target_field S LIKE(source_field) C *LIKE DEFINE target_field source_field[*][*] Center field[*] C EVAL %SUBST(target_field: C %INTH((%SIZE(source_field)C %LEN(%TRIM(source_field))) / 2 + 1): C %LEN(%TRIM(source_field))) = C %TRIM(source_field)

  • #2
    Centering text within a field

    Create a procedure so it can be reused! This example allows for a dynamic input that is not necessarily the same length as the output field.
    Code

    Comment


    • #3
      Centering text within a field

      While I liked seeing Michael's response because I had never used the %int or %inth functions, neither his nor Will's allow for a smaller output string than input string. My preference is to use a procedure also, with a few differences than Will's solution. Also, one advantage that I like about procedures is that you can center back into the same field that you used for input.
      Code

      Comment


      • #4
        Centering text within a field

        I use the following code, which requires no additional fields, and is probably not limited to a range of field sizes; typically I use this with fields in the 60-80 byte range but have tested it with larger fields (up to 1024 bytes) with no problems.
        Code

        Comment


        • #5
          Centering text within a field

          What's the best and shortest way to take a field (i.e. Name) and center the text?

          Comment


          • #6
            Centering text within a field

            I've used the following for about 5 years, so it's well tested. I don't know if it's the "best and shortest", but it's virtually bullet-proof, and it's probably the simplest to use, once the procedure is created. In our shop, we use service programs extensively, so this procedure is one of a handful of "text manipulation" procedures in a single service program. Note that the procedure centers based on the length of the field that is passed, so in some cases, you may want to use the second listed option, depending on the field lengths involved. (Procedure/prototype names used are due to our naming conventions.)
            Code

            Comment

            Working...
            X