Leading zero, each number have same number of characters

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

  • Leading zero, each number have same number of characters

    Hello guys, is it possible to format numbers with leading zero? example:

    operator inputs are from 5,10,15, 100 to 999
    pc-dmis outputs are 005, 010, 015, 100, 999, so string width is always 3 characters ?
    Why am I asking ? by this i can have each pdf filename same width.
    Thank You in advance

  • #2
    Have the operator input 005 instead of 5. Then, if the input comment is C1 you can use this to assign that exact input to a variable ASSIGN/V1=STR(C1.INPUT)
    The STR() function will assign the input as a string of text instead of a numeric value that it always will drop the leading zeros.
    PC-DMIS 2016.0 SP8

    Jeff

    Comment


    • #3
      You can use the FORMAT function. See the PC-Dmis help page below
      Neil Challinor
      PC-DMIS Product Owner

      T: +44 870 446 2667 (Hexagon UK office)
      E: [email protected]

      Comment


      • #4
        You can use a couple of if statements to achieve this effect. For example,

        IF/C1.INPUT < 10
        ASSIGN/V1=STR(0) + STR(0) + (C1.INPUT)
        END_IF/
        IF/C1.INPUT > 9 and c1.input < 100
        ASSIGN/V1=STR(0) + (C1.INPUT)
        END_IF/
        IF/C1.INPUT > 99 and c1.input < 1000
        ASSIGN/V1=C1.INPUT
        END_IF/
        IF/C1.INPUT > 999
        ASSIGN/V1="Hey stoopid, we can't count that high"
        END_IF/

        Comment


        • #5
          If you want a more primitive way to do this over what Neil described, you could create an
          assignment=RIGHT("00" + str(c1.input),3)
          Sometimes PC-DMIS will automatically remove trailing zeros, at least it has for me so you may want to...
          assignment=str(RIGHT("00" + str(c1.input),3))

          Comment


          • #6
            Originally posted by neil.challinor View Post
            You can use the FORMAT function. See the PC-Dmis help page below
            https://docs-dev.hexagonmi.com/pcdmi...earch=function

            example would be

            Code:
            C1         =COMMENT/INPUT,YES,FULL SCREEN=NO,
                        Enter your opperator input
                        ASSIGN/VVV=FORMAT(",%03d,%03",INT(C1.INPUT))
            This will fix the length at 3 characters and pad it out with preceding zero's if the input is less than 3 characters long. So if the operator enters 5 the formatted value would be 005, if they input 10 it would be 010 and if they enter 999 it would be 999.

            Hope this makes sense.
            Neil Challinor
            PC-DMIS Product Owner

            T: +44 870 446 2667 (Hexagon UK office)
            E: [email protected]

            Comment


            • #7
              I will use neil.challinor solution, that is exactly what i was looking for. But Thanks to all anyway, SingularitY tip will be 2nd if neil's fail :-)

              Comment


              • #8
                ended up with:
                Code:
                ASSIGN/VAR1=5.1
                ASSIGN/VAR1X=FORMAT("%04d",INT(VAR1))
                because VAR1 is always converted to Integer, it seems it's working
                output is 4digit width , it's ok for me.
                neil.challinor can You explain to me for what is the 2nd format specificator ?
                Code:
                ,%03

                Comment


                • #9
                  From the PC-Dmis help files

                  Format Specifier for FORMAT Function:

                  The format specifier should have the same syntax as a format specifier used in the sprintf function used in the C++ programming language.

                  A format specifier consists of optional and required fields and has the following syntax:

                  %[flags] [width] [.precision] type

                  Each field of the format specifier is either a single character or a number that signifies a particular format option. The simplest format specifier uses only the percent sign and a type character (for example, %d). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to STDOUT. For example, to print a percent-sign character, use %%.

                  The optional flag, width, and precision fields, which appear before the type character, control other aspects of the formatting. These are described below:

                  flags
                  These optional characters control output justification and the printing of signs, blanks, decimal points, and octal or hexadecimal prefixes. More than one flag can appear in a format specifier.

                  Here are the possible flags:


                  Meaning: Left align the result within the given field width.
                  Default: Right align.

                  +
                  Meaning: Prefix the output value with a sign (+ or –) if the output value is of a signed type.
                  Default: Sign appears only for negative signed values (–).

                  0
                  Meaning: If the width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored.
                  Default: No padding.

                  blank (' ')
                  Meaning: Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear.
                  Default: No blank appears.

                  #
                  Meaning 1: When used with the o, x, or X type, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.
                  Default 1: No prefix appears.

                  Meaning 2: When used with the e, E, or f type, the # flag forces the output value to contain a decimal point in all cases.
                  Default 2: Decimal point appears only if digits follow it.

                  Meaning 3: When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.

                  Default 3: Decimal point appears only if digits follow it. Trailing zeros are truncated. This is ignored when used with d, i, or u.

                  width
                  This second optional field, or argument, controls the minimum number of characters printed. It is a non-negative decimal integer.
                      • If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached.
                      • If the width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
                      • The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if the width is not given, all characters of the value are printed (subject to the precision specification listed below).

                  precision
                  This third optional field, or argument, specifies the number of characters to be printed, the number of decimal places, or the number of significant digits. Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. It is a non-negative decimal integer preceded by a period (.).

                  type
                  This required character determines whether the associated argument is an integer, a double, or a point. The list of available types includes:

                  d - signed decimal integer

                  i - signed decimal integer

                  o - unsigned octal integer

                  u - unsigned decimal integer

                  x - unsigned hexadecimal integer, using "abcdef"

                  X - unsigned hexadecimal integer, using "ABCDEF"

                  e - double in exponential form [-]d.dddd e [sign]ddd

                  E - same as e, except uses E to introduce the exponent

                  f - double with the form [-]dddd.dddd

                  g - formats to either the e or f format depending on which is more compact

                  G - same as g, except uses E to introduce the exponent
                  Neil Challinor
                  PC-DMIS Product Owner

                  T: +44 870 446 2667 (Hexagon UK office)
                  E: [email protected]

                  Comment


                  • #10
                    I read it ofc, so:
                    0
                    Meaning: If the width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored.
                    Default: No padding.
                    in 1st format specification,
                    Code:
                    "%03d,
                    you declared its integer (d), leading zero to width of 3 chars, just i dont understand why we need 2nd format spec.
                    Code:
                    %03",

                    Comment


                    • #11
                      The 2nd format (width) adds the leading zeros (if necessary) whereas the 3rd (precision) format is supposed to truncate the input if it contains more characters than the width allows.

                      Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.
                      It looks like you can get away with not using it in your case - just wanted to supply an example that showed the full command format.
                      Neil Challinor
                      PC-DMIS Product Owner

                      T: +44 870 446 2667 (Hexagon UK office)
                      E: [email protected]

                      Comment


                      • mb0258
                        mb0258 commented
                        Editing a comment
                        just wanted to supply an example that showed the full command format.
                        Okey, closed, thank again all.

                    Related Topics

                    Collapse

                    Working...
                    X