Xbasic

CHKDIGIT Function

Syntax

Checksum_Character as C = CHKDIGIT(C character[,N option])

Arguments

character

A string of numbers in character format.

option

Numeric. Optional. A number that indicates the type of checksum to calculate.

0 = Credit Card (Default)
1 = PostNet
2 = UPC

Description

Returns a checksum digit for a string of numbers.

Discussion

CHKDIGIT() returns a Checksum_Character that is the checksum digit for a string of numbers contained in the Input_String parameter. If the Input_String is invalid or NULL, CHKDIGIT() returns an empty character string. If the Option_Number is invalid, CHKDIGIT() returns "0". The checksum of a number is computed using the following method: starting with the right-most digit in the number and moving from right to left, multiply each number by either 2 or 1. The first number is multiplied by 2, the next by 1, the next by 2, and so on. Then add all the digits in the resulting number, and subtract the sum from the next highest multiple of ten. For example, the checksum for "2175" is computed as follows:

Base number: 2 1 7 5
Multiply each number in base by corresponding number: 1 2 1 2
Result: 2 2 7 10
Sum digits: 2 + 2 + 7 + 1 + 0 = 12
Next highest multiple of ten: 20
Check digit: 20 - 12 = 8

Example 

dim cs as C
cs = "2175"
CHKDIGIT(cs) -> 8

See Also