12

Since a number can also be a decimal this makes me think that a CC number should be an integer. This would make sense as I don't think any credit cards start with 0 and since they all follow the same sort of pattern:

4444333322221111

So I guess they're an integer but I'm not too sure what international cards are like? Do any start with 0?

Update

Thanks for all your responses. It's less to store them (in fact I'd only store the last 4 numbers) and more to do a quick validation check. Regardless, I'd just treat it as an integer for validation, i.e. making sure that it's between 13-16 digits in length and always never a decimal.

Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87
  • 3
    If you are dealing with CC numbers why not look at whatever API you are interfacing to when dealing with them and use that data format? – Sign Sep 01 '11 at 11:25

6 Answers6

40

Credit card numbers are not strictly numbers. They are strings, but the numbers which make up the long 16 digit number can be exploded in order to validate the number by using the checksum of the number.

You aren't going to be doing any multiplication or division on the CC number, so it should be a string in my opinion.

Quick Prefix, Length, and Check Digit Criteria

CARD TYPE      |    Prefix  |   Length  | Check digit algorithm
-----------------------------------------------------------------
MASTERCARD     |    51-55   |   16      |    mod 10
VISA           |    4       |   13,  16 |    mod 10
AMEX           |    34/37   |   15      |    mod 10
Discover       |    6011    |   16      |    mod 10
enRoute        | 2014/2149  |   15      |    any
JCB            |     3      |   16      |    mod 10
JCB            |  2131/1800 |   15      |    mod 10
Layke
  • 51,422
  • 11
  • 85
  • 111
  • 3
    7 years later the table is still impressive. – VTodorov Apr 13 '18 at 13:57
  • This answer is old, but I want to point out that if you store a card number as a varchar vs. a decimal in SQL Server, there is a HUGE storage difference. A varchar(16) will take 16 bytes of storage vs. 9 bytes for a decimal(16, 0). Obviously for a small set of data, this isn't a big deal, but for 10s of millions of card numbers, the storage difference is significant. – user2966445 Jul 27 '18 at 16:34
  • Nine years later. Want to see if a card fits in a range. Storing as a number allows greater than or less than comparison – Wes Mar 31 '20 at 21:45
4

Don't use an integer for this.

Depending on the size of your integers (language/machine dependent), they may be too large to store as integers.

The use of credit card numbers is also not as integers, as there's no reason for doing arithmetic with them.

You should generally regard them as arrays of decimal digits, which might most easily be stored as strings, but might merit an actual array, again depending on your programming language.

They also contain encoded banking authority information as described in the wikipedia article on Bank Card Numbers, and are a special case of ISO/IEC 7812 numbers, which in fact can start with zero (though I don't think any credit cards do). If you need this information, a CC number might actually merit it's own data type, and likely some banks implement one.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
3

Better to use an array of single-digit ints. Often the individual digits are used in some type of checksum to validate the credit card number. It would also take care of the case that a CC# actually starts with 0.

For example,

int[] cc = { 4, 3, 2, 1 }
bool Validate(int[] cc)
{
   return ((cc[0] + 2*cc[1] + 6*cc[2]) % 5) == 0;
}

Something like that, although the equations they use are more complex. This would be a lot harder (i.e. would require division and truncation) with just

int cc = 4321;


Edit:
Keep in mind also, that each digit in a credit card number means something. For example, the 3rd and 4th digits might represent the state in which the card was made, and the 5th digit might be an index to the particular bank that issued the card, for example.

user807566
  • 2,828
  • 3
  • 20
  • 27
  • can you give an example for where this would be necessary? – Thilo Sep 01 '11 at 11:25
  • @Thilo: all types of checksum validation. Google gives you a lot on that. – Niklas B. Sep 01 '11 at 11:35
  • All of this would be possible with an integer or a string as well. – Thilo Sep 01 '11 at 11:40
  • 1
    @Thilo: That's true. However, an integer would not be able to store leading 0's and would require dividing by `10^n` to get the `nth` digit. Strings are essentially character arrays, and would be a viable option, however they'd also require converting char_to_int any time you want to do a mathematical operation (like a checksum). – user807566 Sep 01 '11 at 11:44
2

Personally, I always store them as a string... it is a series of integers, much like a telephone number, not one big integer itself.

  • 1
    wouldn't a series of integers rather be expressed by an array of integers? (or possibly as an array of chars, but not in the sense of an ASCII string) – Niklas B. Sep 01 '11 at 11:33
  • 1
    Technically, yes, but it seems to me rather like overkill... depends whether you're trying to get 100% performance from your system or make it more developer friendly. – tommy5dollar Sep 01 '11 at 11:40
  • 1
    You often have to work with the actual integer values when validating CC numbers. So I find it more convenient to store them as integers right away instead of converting the digits to int before every operation. But this is more of a matter of taste. – Niklas B. Sep 01 '11 at 11:51
  • 1
    @tommy5dollar: The most developer-friendly application would be to have a `CreditCard` class with interfaces to access the numbers, rather than requiring each client function to parse a string. – user807566 Sep 01 '11 at 11:54
1

I would imagine that they are integers as they (almost certainly) do not contain any alphabetic characters and never have any decimal places.

Mus
  • 7,290
  • 24
  • 86
  • 130
0

No credit/debit card numbers start with a zero (probably due to discussions like this).

All credit/debit card numbers have a check digit that is calculated using the Luhn algorithm

As it happens, 4444333322221111 passes the Luhn check digit test.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I cannot find the source again, but I have looked this up sometime and found that certain credit card numbers can start with a 0. – Timo Sep 02 '16 at 13:19