Custom-Pad a Number
Pad any number with custom characters to a target length. Left-pad, right-pad, or center-pad with zeros, spaces, or any character.
About
String padding errors cause silent data corruption in databases, CSV exports, and API payloads. A ZIP code of 7310 instead of 07310 routes mail to the wrong state. An invoice number missing its leading zeros breaks sort order across accounting systems. This tool applies deterministic character padding to any numeric string, letting you specify the fill character c, target width w, and alignment direction. It handles edge cases that naive implementations miss: inputs already exceeding target width are returned unchanged, and center-padding distributes the remainder with floor on the left and ceil on the right to maintain visual balance.
The tool preserves sign characters and decimal points as part of the input string. It does not interpret numeric value. This is intentional. Padding operates on the string representation, not the number. If you need to pad -42 to width 6 with zeros, the result is 00-42, not -00042. For sign-aware padding, pre-process by separating the sign before padding the magnitude.
Formulas
Padding computes the number of fill characters needed as the difference between the target width and the current string length, clamped to zero.
For left-padding, all padding characters are prepended. For right-padding, all are appended. For center-padding, the distribution is:
Where w is the desired total width, s is the input string, c is the pad character (single character), and len returns the string length. When len(s) β₯ w, the original string is returned unmodified.
Reference Data
| Use Case | Input | Pad Char | Width | Direction | Result |
|---|---|---|---|---|---|
| US ZIP Code | 7310 | 0 | 5 | Left | 07310 |
| Invoice Number | 42 | 0 | 8 | Left | 00000042 |
| Binary Byte | 101 | 0 | 8 | Left | 00000101 |
| Hex Color Code | FF | 0 | 6 | Left | 0000FF |
| Fixed-Width Column | 99 | Space | 10 | Right | 99Β·Β·Β·Β·Β·Β·Β·Β· |
| Centered Label | 42 | - | 10 | Center | ----42---- |
| Centered Odd Remainder | 7 | * | 6 | Center | **7*** |
| SSN Fragment | 56 | 0 | 4 | Left | 0056 |
| Already Full Width | 12345 | 0 | 3 | Left | 12345 |
| Decimal Price | 9.5 | 0 | 6 | Left | 0009.5 |
| Batch ID | 3 | 0 | 12 | Left | 000000000003 |
| Table Separator | 100 | . | 8 | Right | 100..... |
| Empty Input | (empty) | 0 | 4 | Left | 0000 |
| Single Digit Year | 5 | 0 | 2 | Left | 05 |
| Product SKU | 891 | 0 | 10 | Left | 0000000891 |