padding string with zeros

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf padding string with zeros

  • Creator
    Topic
  • #51175
    Gary Atkinson
    Participant

    I need to pad a string with leading zeros if the length is less than 10.  So, if receive 12356…I need to send out 0000123456.  This is in PID:3 for the Medical record number.  Is there a tcl snippet I can use to do this and add into my xlate?

Viewing 18 reply threads
  • Author
    Replies
    • #69041

      I often do something like this for padding:

      Code:

      if {[lindex $xlateInVals 0] != “”} {set xlateOutVals [list [format “%05s” [lindex $xlateInVals 0]]]}

      It’s just an example that you’d need to modify for your own needs.

      -- Max Drown (Infor)

    • #69042
      Gary Atkinson
      Participant

      Let me add one more thing.  The inbound string could be any vary of numbers up to 10 digits.  So it may be 123, 1234, 123456 etc.  The output will need to be padded with zeros, so the string is 10 digits total.

    • #69043
      Gary Atkinson
      Participant

      Never mind I figured it out.  Thx Max for showing me the way…  😆

    • #69044
      Tom Rioux
      Participant

      Gary,

      You can use Max’s code to do this by simply changing the “%05s” to

      “%010s”.   This will convert your MRN, regardless of length (if less than 10) and convert it to a 10 digit number with leading zeroes.

      Hope this helps…

      Tom

    • #69045
      Gary Atkinson
      Participant

      Thx.  I had to “re-read” the doc on the format command.  I love the syntax docs, because they are so easy to read for us “non-programmer” types  🙄

    • #69046
      Mahmoud Ihaddadene
      Participant

      Hi,

      Input= 12.34

      set xlateOutVals [format “%0.3f” [lindex $xlateInVals 0]]

      Output=12.340

      My question: How can i padded a zero at the left to have always on the Output 012.340

      Thanks

      Moe

    • #69047
      Rob Abbott
      Keymaster

      format “0%0.3f” $val

      Rob Abbott
      Director, Product Management - Infor Cloverleaf

    • #69048

      Gary Atkinson wrote:

      Thx.

      -- Max Drown (Infor)

    • #69049
      Mahmoud Ihaddadene
      Participant

      Rob Abbott wrote:

      format “0%0.3f” $val

      I need to format the Input to xxx.xxx (decimal lengh 7).

      Example1: Input= 12  Output should be=012.120

      Example2: Input=12.5 Output should be=012.500

    • #69050

      Mahmoud Ihadadene wrote:

      Rob Abbott wrote:

      format “0%0.3f” $val

      I need to format the Input to xxx.xxx (decimal lengh 7).

      Example1: Input= 12

      -- Max Drown (Infor)

    • #69051
      Mahmoud Ihaddadene
      Participant

      Thank you

    • #69052
      Mahmoud Ihaddadene
      Participant

      Hi Max,

      If my input is = 1.23 the Output wil be : 01.230 !!!! but me i need to get 001.230

      Regards

    • #69053

      That’s a good question. Maybe you could run the number through formatting twice? Once of the right side and once for the left side?

      -- Max Drown (Infor)

    • #69054
      Tom Rioux
      Participant

      Try changing Max’s original format code to:

      set output [format “0%0.3f” $var]

      That should give you what you need.

      Hope this helps…

      Tom Rioux

    • #69055
      Jim Kosloskey
      Participant

      Why not:

      format “00%0.3f” $stuff

      where $stuff is your value?

      email: jim.kosloskey@jim-kosloskey.com

    • #69056

      That will always add two zeros to the left side (“0012.000”).

      -- Max Drown (Infor)

    • #69057

      This seems to work I think …

      lappend input “12”
      lappend input “12.15”
      lappend input “1.23”

      #xxx.xxx

      foreach var $input {
      [code]lappend input “12”
      lappend input “12.15”
      lappend input “1.23”

      #xxx.xxx

      foreach var $input {

      -- Max Drown (Infor)

    • #69058
      Chris Williams
      Participant

      /beginCharlieMode

      A single format command will provide the desired output:

      Code:

      format “%07.3f” 1.23


      The 0 specifies leading zeros

      The 7 specifies the TOTAL output field width including the decimal point (NOT just the number of digits to the left of the decimal)

      The .3 specifies the number of places to the right of the decimal

      The f specifies that this is a floating point number

      /endCharlieMode

      Cheers.

    • #69059

      Chris Williams wrote:

      A single format command will provide the desired output:

      Code:

      format “%07.3f” 1.23


      The 0 specifies leading zeros

      The 7 specifies the TOTAL output field width including the decimal point (NOT just the number of digits to the left of the decimal)

      The .3 specifies the number of places to the right of the decimal

      The f specifies that this is a floating point number

      Cheers.

      That does appear to work perfectly. Thanks, Chris!

      Code:

      lappend input “12”
      lappend input “12.15”
      lappend input “1.23”

      #xxx.xxx

      foreach var $input {
         set output [format “%07.3f” $var]
         #set output [format “%0.3f” $var]
         #set output [format “%07s” $output]
         puts “$var: $output”

      #Result:
         #12: 012.000
         #12.15: 012.150
         #1.23: 001.230
      }

      -- Max Drown (Infor)

Viewing 18 reply threads
  • The forum ‘Cloverleaf’ is closed to new topics and replies.

Forum Statistics

Registered Users
4,966
Forums
28
Topics
9,106
Replies
33,627
Topic Tags
248