ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Trimming leading and trailing zeros

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trimming leading and trailing zeros

    Does anyone have any ideas for a routine to trim leading and trailing zeros from a numeric field that is zoned(7:4) and plop them into a character field.

    Thanks

  • #2
    So you want to make the number '00100' into just '1'? Why would you want to do that?

    Well, I guess its not difficult.

    Code:
    charFld = %trim( %char( numFld ) : '0.,');

    Comment


    • #3
      if you want to keep the decimal point, just code '0' for the second parameter of %TRIM. You might need some extra coding if you want the value 000.0000 to still have one '0' and not be just '' (empty string) or '.'.

      But I agree with Scott that this is a weird thing to do. We might be able to suggest a better solution than trimming '0's if we knew what the real requirement is.

      Comment


      • #4
        The company I work for has an odd nomenclature for determining the Material ID, given the 3 fields GAUGE, defined as packed(7,4), WIDTH, defined as packed(7,4) and ALLOY is character(6). The Material ID has been calculated manually in the past and stored in a table. They want me to come up with an algorithm to programmatically calculate the material id given the 3 fields I've mentioned.

        Here are some examples of the 3 fields and the expected results:

        when gauge = 000.0320, width = 048.0000 alloy = '3003 ' => MATERIAL ID = 0.032483003
        when gauge = 000.0400, width = 036.0000 alloy = '3003 ' => MATERIAL ID = 0.04363003
        .
        .
        .
        when gauge = 010.0000, width = 048.0000, alloy = ' ' => MATERIAL ID = 1048
        .
        .
        when gauge = 018.0000, width = 048.0000. alloy = 'T316L ' => MATERIAL ID = 1848T316L
        when gauge = 000.0000, width = 001.4375 alloy = 'T304 ' => MATERIAL ID = 01.4375T304


        This is a bizarre naming convention, I realize, but it's beyond my control

        Thanks again

        Comment


        • #5
          Wow, that is bizarre.

          If I understand correctly, to restate your requirement:
          • Always trim trailing zeros from the decimal places: '123.4500' -> '123.45'
          • Trim the decimal point if the decimal places are all zero: '123.0000' -> '123'
          • Trim leading zeros, but don't trim the final zero before the decimal point: '000.1234' -> '0.1234'
          Here's how I would do it. You might be able to combine some of these into one statement. But I would use several statements like this just because it's easier (for me) to understand, and it would be easier to debug.

          Untested:
          Code:
          dcl-s str varchar(10);
          
          str = %char(num); // get 00123.5600 as the string '123.4500'
          str = %trimr(str : '0'); // Trim trailing zeros from the decimal places
          str = %trimr(str : '.'); // Trim the decimal point if the decimal places were all zero
          if %subst(str : 1 : 1) = '.'; // If the first character is the decimal point
             str = '0' + str; // Change '.123' to '0.123'
          endif;
          Edit: I corrected the code above to fix the errors found by Scott below: I removed the stray quote from the second %trimr, and fixed the IF statement. (But as Scott said, the IF statement is unnecessary if DECEDIT('0.') is used.)
          Last edited by Barbara Morris; September 28, 2020, 04:24 PM. Reason: Edit: See my comments at the end of the post about my edits.

          Comment


          • #6
            Barbara,

            You have an extra quote mark on the line where you're trimming the trailing period, and your IF statement should be comparing to '.' (no point in adding a zero otherwise.)

            Or you could use DECEDIT to add the extra leading zero... might be a bit simpler.

            Code:
            ctl-opt decedit('0.');
             .
             .
            dcl-s str varchar(20);
            
            str = %trimr(%trimr(%char(num):'0'):'.');

            Comment


            • #7
              Just wondering why you ask the same question twice?

              Does anyone have any ideas for a routine to trim leading and trailing zeros from a numeric field that is zoned(7:4) and plop them into a character field. Thanks

              Comment


              • Scott Klement
                Scott Klement commented
                Editing a comment
                I assume it was a mistake. It'd be nice if we could clean up these mistakes.

            • #8
              Originally posted by Peder Udesen View Post
              Just wondering why you ask the same question twice?

              https://code400.com/forum/forum/iser...trailing-zeros
              I was aware that it was out there twice. I realized it right after I hit ENTER the 2nd time, but I have no capability to delete even my own posts. I have requested the ability to do so several times to no avail. I would be happy to perform cleanup.

              Comment

              Working...
              X