Check Digit Formula

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Check Digit Formula

  • Creator
    Topic
  • #52690
    Alka Sharma
    Participant

      I am tasked with calculating a check digit formula for a visit number which comes in ZPV segment. I am able to do all the steps except the step 7.

      I am hoping someone out there has done this and will be able to help me out.

      In excel there is a ceiling function and I did find a ceil in tcl but unable to make it work.

      Below are the steps:

      CHECK DIGIT FORMULA FOR PATIENT ACCOUNT NUMBER

      The patient account number is 8 digits long.  The formula uses the first 7 digits to validate the 8 position.

      The following patient account number will be used as an example: 61562419

      1.  Take the first 7 positions of the patient account number: 6156241

      2.  Replace digits in positions 2, 4 and 6 with zeros: 6050201

      3.  Multiply result from step2 by 2: 6050201 * 2 = 12100402

      4.  Add the digits from the results of step 3 together: 1+2+1+0+0+4+0+2 = 10

      5.  Add positions 2, 4 and 6 from step 1 together: 1 + 6 + 4 = 11

      6.  Add results from steps 4 and 5 together: 10 + 11 = 21

      7.  Round the result from step 6 to the next highest multiple of 10:

      30

    Viewing 3 reply threads
    • Author
      Replies
      • #75130
        Keith McLeod
        Participant

          I am sure there is a more elegant way, but try something like this:

          set X 21

          expr ($x + 10) – ($x + 10) % 10

          This should give you the desired result.  The Value plus 10 gets you beyond the next multiple of 10 and that value modulus 10 is subtracted to remove any remainder leaving you at the rounded value…Hope this helps.

        • #75131
          Charlie Bursell
          Participant

            I don’t beleive in re-inventing wheels   😀

            Sounds like EAN-13 validation like used in barcodes

            One place I have seen it done is at http://wiki.tcl.tk/10538

            Take what you get from the Wiki with a grain of salt.  Similar to some of the stuff you get here  ðŸ˜€

          • #75132
            Chris Williams
            Participant

              You don’t mention what type of check digit. We work with McKesson which can use both MOD-10 and MOD-11. Here is the code we use for a MOD-10 check digit using the Luhn algorithm:

              Code:

              proc checkDigit10 {number} {

                 set tot 0
                 set sub 0
                 set ex 0
                 set wy 0
                 set checkDigit “”

                 if {![lempty $number] && [string is digit $number] } {

                   # trim the input value

                     set number [string trim $number]

                   # Split the number into individual digits

                     set numList [split $number {}]

                   # Work the digits of the number from right to left

                     for {set ex [expr [llength $numList] -1]} {$ex >= 0} {incr ex -1} {

                       # Multiply every other number by 2 beginning with right-most digit

                         if [expr $wy % 2 == 0] {
                             set sub [expr [lindex $numList $ex] * 2]
                         } else {
                             set sub [lindex $numList $ex]
                         }

                       # If more than 1 digit then add them to the total individually

                         set subList [split $sub {}]
                         foreach itm $subList {
                             incr tot $itm
                         }

                         incr wy

                     }

                   # Subtract the $tot value from the next integer multiple of 10.

                     set checkDigit [expr (10-($tot % 10)) % 10]

                 }

                 return $checkDigit

              }


              ~

            • #75133
              Alka Sharma
              Participant

                Thanks everyone.

                Chris,

                I am waiting for the company to let me know if it is mod10 or 11 verification. Thanks for the code

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