Newbies maxrcul Posted May 2, 2007 Newbies Posted May 2, 2007 Try as I may, I can't find any threads on scripts for calculating checksums for 5, 9 and 11 digit zip codes. I don't want to create the bar code just the numbers representing the code including checksum.
comment Posted May 2, 2007 Posted May 2, 2007 I don't think 5 and 9 digit codes have a checksum. Only the 11 digits version has a check digit, and I believe it's a regular Luhn (modulo 10) algorithm.
Newbies maxrcul Posted May 2, 2007 Author Newbies Posted May 2, 2007 To calculate a mod-10 checkdigit: 1) Sum the odd-digits from left to right and multiply the total by 3. 2) Sum the even-digits from left to right and add to the total in 1) 3) The check digit is the number needed to round up to the next even multiple of 10. e.g. If the total from 1) and 2) is 74, then the MOD10 checksum would be 6 Do you have script to do this within FM?
comment Posted May 2, 2007 Posted May 2, 2007 You would do this with a custom function, or a repeating calculation field. But now, I don't know: according to the entry in Wikipedia, it's not the Luhn algorithm, but just plain: 10 - Mod ( sumOfDigits ; 10 ) OTOH, this is plainly wrong, because if the sum of digits is 0 or a multiple of 10, the result is "10" - and that's two digits.
Newbies maxrcul Posted May 2, 2007 Author Newbies Posted May 2, 2007 CALCTOT Calculation(Number) = TextToNum(Left(ZIP,1)) + TextToNum(Middle(ZIP,2,1)) + TextToNum(Middle(ZIP,3,1)) + TextToNum(Middle(ZIP,4,1)) + TextToNum(Middle(ZIP,5,1)) + TextToNum(Middle(ZIP,6,1)) + TextToNum(Middle(ZIP,7,1)) + TextToNum(Middle(ZIP,8,1)) + TextToNum(Middle(ZIP,9,1)) CHECKSUM Calculation(Number) = 10 - MOD(CALCTOT,10) This should work but does FM have a text to num function? Not sure why the wiki says to sum the odds and evens and add them together.
mr_vodka Posted May 2, 2007 Posted May 2, 2007 You may want to check out this CF. http://www.briandunning.com/cf/87 It will need these two in conjunction http://www.briandunning.com/cf/88 http://www.briandunning.com/cf/90
comment Posted May 2, 2007 Posted May 2, 2007 Not sure why the wiki says to sum the odds and evens and add them together. That is the Luhn algorithm. The article about ZIP codes describes a method that does NOT use the Luhn algorithm.
Jeep Watson Posted August 5, 2007 Posted August 5, 2007 max... coming in late to this discussion, but it's one I've been dabbling with for a year and a half... 1 - a checksum IS required for all zips whether they be 5,9, or 11 digits 2 - the explanation directly from the USPS Publication 25 Chapter 4... "Correction Code: Whether it represents five-, nine-, or eleven-digit ZIP Code information, the POSTNET barcode is always printed in a format that begins and ends with a frame bar (full or tall bar). To ensure POSTNET accuracy during mail processing, a correction character (five bars) must be included immediately before the rightmost frame bar of all POSTNET barcodes (see exhibit 5-3). The correction character is always the digit that, when added to the sum of the other digits in the barcode, results in a total that is a multiple of 10. For example, the sum of the ZIP+4 barcode 12345-6789 is 45. Adding a correction character of 5 results in the sum of the 10 digits being a multiple of 10." 3 - The first part, summing the digits - The Luhn Algorithm may or may not work but it seems like a rather odd approach for a non-mathematician. The most elegant solution for the Sum of the Digits I've come up with so far is: Let ( n= ZipStrip ; Evaluate ( Substitute (n ; [ "0" ; "0+" ] ; [ "1" ; "1+" ] ; [ "2" ; "2+" ] ; [ "3" ; "3+" ] ; [ "4" ; "4+" ] ; [ "5" ; "5+" ] ; [ "6" ; "6+" ] ; [ "7" ; "7+" ] ; [ "8" ; "8+" ] ; [ "9" ; "9+" ] ) & "0" ) ) where ZipStrip is a previously created calc to strip unwanted chars: Filter ( Zip ; "1234567890" ) If anyone has a shorter, more elegant way, I'd love to see it... 4 - The other MAJOR part of the solution is to calculate the Checksum or Checkdigit. I've done calcs that are 60 or 70 lines long that worked - I've seen approaches by others that were very similar. But they all seemed overly long and complex for something that should be able to be expressed as a rather simple mathematical statement. It took me a long time and a lot of research to come up with but here is the most elegant solution to this part: Mod(10-Mod(calc_SumDigits; 10); 10)
Jeep Watson Posted August 5, 2007 Posted August 5, 2007 to all... I've also played around with trying to create a recursive custom function for the Sum of the Digits but haven't been able to work it out. Here's the last approach I worked with but if you try it, you'll see it's close but no cigar. (recursion makes me dizz!y). Anybody have any ideas? Let ( len = Length ( num ) ; Case ( len > 1 ; SumDigits ( Left ( num ; len-1 ) ) + Left ( num ; len ) ; num ) )
comment Posted August 5, 2007 Posted August 5, 2007 The first part, summing the digits - The Luhn Algorithm may or may not work but it seems like a rather odd approach for a non-mathematician. The Luhn algorithm is not an algorithm for summing the digits. It is an algorithm for producing a check digit. As your quote from the USPS Publication confirms, the required method to produce the correct check digit is NOT the Luhn algorithm. Using the Luhn algorithm here would produce the wrong check digit. For myself, I would use a custom function to sum the digits, but your method should work too, esp. for someone who doesn't have Advanced. Here's a simple custom function to sum the digits: SumDigits ( number ) Let ( [ units = Int ( Mod ( number ; 10 ) ) ; nextNumber = Div ( number ; 10 ) ] ; units + Case ( nextNumber ; SumDigits ( nextNumber ) ) ) The other MAJOR part of the solution is to calculate the Checksum or Checkdigit. Try: Mod ( - sumOfDigits ; 10 )
Jeep Watson Posted August 6, 2007 Posted August 6, 2007 hey comment... that's great! I think I like the custom function better - it's a little easier to plug into other solutions. I'll have to spend some time and try to get my head around the Div function and how Mod and Div work in conjunction in the custom function. Are they used together like this frequently?
Recommended Posts
This topic is 6380 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now