Clamp Integer Digits
Clamp the number of digits in an integer by padding or truncating to fit within a specified minimum and maximum digit count.
About
Controlling the digit count of an integer is a routine requirement in data formatting, protocol compliance, and database field constraints. A value with too few digits fails fixed-width validation (ISO 7812 card numbers, ZIP codes, industrial part IDs). A value with too many overflows storage buffers or breaks display layouts. This tool applies a digit-count clamp: if the integer has fewer than dmin digits, it left-pads with a fill character (typically 0); if it exceeds dmax digits, it truncates from the chosen end. Negative sign is preserved and excluded from the digit count. The tool assumes base-10 representation. Note: truncation is a lossy operation and the resulting value may differ numerically from the original.
Formulas
Given an integer n with digit count d (excluding sign), minimum digit constraint dmin, and maximum digit constraint dmax:
Where pad(n, dmin) prepends the fill character c repeatedly until the digit string reaches length dmin. The padding count p is computed as:
Where trunc(n, dmax) removes excess digits. If truncation direction is left (most significant), the last dmax digits are kept. If right (least significant), the first dmax digits are kept.
Constraint: dmin ≤ dmax. If violated, the operation is rejected. n - the input integer (string representation). d - current digit count. c - pad fill character (default 0).
Reference Data
| Use Case | Typical Min Digits | Typical Max Digits | Pad Char | Standard / Context |
|---|---|---|---|---|
| US ZIP Code | 5 | 5 | 0 | USPS Addressing Standard |
| ZIP+4 | 9 | 9 | 0 | USPS Extended |
| Credit Card (PAN) | 13 | 19 | 0 | ISO/IEC 7812 |
| EAN-13 Barcode | 13 | 13 | 0 | GS1 General Spec |
| UPC-A Barcode | 12 | 12 | 0 | GS1 US |
| ISBN-13 | 13 | 13 | 0 | ISO 2108 |
| Unix Timestamp (sec) | 10 | 10 | 0 | POSIX (until 2286) |
| Unix Timestamp (ms) | 13 | 13 | 0 | JavaScript Date.now() |
| IBAN (Numeric Part) | 2 | 30 | 0 | ISO 13616 |
| IPv4 Octet | 1 | 3 | 0 | RFC 791 |
| HTTP Status Code | 3 | 3 | 0 | RFC 7231 |
| SQL INT (signed) | 1 | 10 | - | Max 2147483647 |
| SQL BIGINT (signed) | 1 | 19 | - | Max 9223372036854775807 |
| US Phone (digits only) | 10 | 11 | 1 | NANP E.164 |
| Part Number (industrial) | 6 | 12 | 0 | Varies by manufacturer |
| DUNS Number | 9 | 9 | 0 | Dun & Bradstreet |
| RGB Channel (decimal) | 1 | 3 | 0 | Range 0 - 255 |
| Binary Byte | 8 | 8 | 0 | 8-bit fixed width |
| Hex Color (no #) | 6 | 6 | 0 | CSS Hex Color |
| SSN (digits only) | 9 | 9 | 0 | US SSA |