The quickest way to format numbers as two digits in TypeScript is String(n).padStart(2, "0"), which turns 7 into "07" and leaves 42 alone. If you’d rather use a built-in formatter, set minimumIntegerDigits: 2 in Intl.NumberFormat:
const twoDigits = (n: number): string => String(n).padStart(2, "0");
twoDigits(7); // "07"
Nothing below is pseudo-code: I built each file under TypeScript 7.0.2 and Node.js v22.22.2 with --strict and copied the console text as it printed.
You’ll also see two-digit dates, timers, the bugs with negatives and big values, and two decimal places.
Pad a number to two digits with padStart()
padStart() is a string method, so you convert the number first. It only adds zeros when the string is shorter than the target length:
const twoDigits = (n: number): string => String(n).padStart(2, "0");
for (const n of [0, 7, 9, 10, 42, 125]) {
console.log(n, "->", twoDigits(n));
}
Output:
0 -> 00
7 -> 07
9 -> 09
10 -> 10
42 -> 42
125 -> 125
Notice 125 stays "125". Padding never cuts digits off. I go deeper into widths like 5 or 8 digits, IDs and invoice numbers in converting a number to a string with leading zeros in TypeScript.
Format two digits with Intl.NumberFormat and minimumIntegerDigits
Intl.NumberFormat takes a minimumIntegerDigits option from 1 to 21. Build it once and reuse it:
const twoDigit = new Intl.NumberFormat("en-US", {
minimumIntegerDigits: 2,
useGrouping: false,
});
for (const n of [3, 12, 7.5, -5, 1250]) {
console.log(String(n).padEnd(5), "->", twoDigit.format(n));
}
const grouped = new Intl.NumberFormat("en-US", { minimumIntegerDigits: 2 });
console.log("with grouping:", grouped.format(1250));
console.log("toLocaleString:", (4).toLocaleString("en-US", { minimumIntegerDigits: 2 }));
Output:
3 -> 03
12 -> 12
7.5 -> 07.5
-5 -> -05
1250 -> 1250
with grouping: 1,250
toLocaleString: 04

useGrouping is false.Decimals keep their fraction, so 7.5 becomes "07.5", and grouping is on by default. If you only need it once, toLocaleString() accepts the same options. For plain conversions, see how to convert a number to a string in TypeScript.
How do you format dates and times with two digits?
getMonth() is zero-based, so add 1 before padding. You can pad each part yourself or ask Intl.DateTimeFormat for "2-digit" fields:
const d = new Date(2026, 9, 5, 9, 4, 7); // Oct 5, 2026 9:04:07 AM (local time)
const pad = (n: number): string => String(n).padStart(2, "0");
console.log("manual: ", `${pad(d.getMonth() + 1)}/${pad(d.getDate())}/${d.getFullYear()}`);
console.log("24-hour: ", `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`);
const dateFmt = new Intl.DateTimeFormat("en-US", {
month: "2-digit", day: "2-digit", year: "numeric",
});
console.log("Intl date:", dateFmt.format(d));
const timeFmt = new Intl.DateTimeFormat("en-US", {
hour: "2-digit", minute: "2-digit", timeZone: "America/Chicago",
});
console.log("Intl time:", timeFmt.format(d));
console.log("hour12 off:", d.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hour12: false }));
Output:
manual: 10/05/2026
24-hour: 09:04:07
Intl date: 10/05/2026
Intl time: 09:04 AM
hour12 off: 09:04

Intl.DateTimeFormat give the same MM/DD/YYYY result; set hour12: false for a 24-hour clock.I set timeZone because a server’s clock may sit in a different zone than your users in Chicago or Denver. For more patterns, read how to format dates in TypeScript and formatting a date as MM/DD/YYYY.
Build a two-digit timer from seconds
Countdown clocks and workout timers need mm:ss. Split the seconds, then pad minutes and seconds:
function formatDuration(totalSeconds: number): string {
const h = Math.floor(totalSeconds / 3600);
const m = Math.floor((totalSeconds % 3600) / 60);
const s = totalSeconds % 60;
const mmss = `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
return h > 0 ? `${h}:${mmss}` : mmss;
}
for (const secs of [5, 125, 600, 3725]) {
console.log(secs, "seconds ->", formatDuration(secs));
}
Output:
5 seconds -> 00:05
125 seconds -> 02:05
600 seconds -> 10:00
3725 seconds -> 1:02:05
Hours stay unpadded here, the way most players show 1:02:05. If your seconds come from two timestamps, calculating the difference between dates in TypeScript shows how to get them.
Common mistakes when formatting two digits
The old ("0" + n).slice(-2) trick works for 0-9, but here’s what it does with other values:
const slice2 = (n: number): string => ("0" + n).slice(-2);
const pad2 = (n: number): string => String(n).padStart(2, "0");
const intl2 = new Intl.NumberFormat("en-US", { minimumIntegerDigits: 2 });
for (const n of [7, 125, -3]) {
console.log(`${n}: slice "${slice2(n)}" | padStart "${pad2(n)}" | Intl "${intl2.format(n)}"`);
}
Output:
7: slice "07" | padStart "07" | Intl "07"
125: slice "25" | padStart "125" | Intl "125"
-3: slice "-3" | padStart "-3" | Intl "-03"
slice(-2) silently turns 125 into "25", which is a real bug in an order count. Negatives also differ: padStart sees "-3" as already two characters, while Intl gives "-03".
The other trap is types. Strict mode won’t let you call string methods on a number or store a formatted string in a number:
const month: number = 7;
const label = month.padStart(2, "0");
const price: number = (19.9).toFixed(2);
Output:
two_types.ts(2,21): error TS2339: Property 'padStart' does not exist on type 'number'.
two_types.ts(4,7): error TS2322: Type 'string' is not assignable to type 'number'.

padStart on a number and TS2322 because toFixed() returns a string.Format at the last moment, right before display, and keep the number for math. If text comes back from a form, converting a string to a number in TypeScript covers the safe way back.
Format numbers with two decimal places in TypeScript
Sometimes “two digits” means cents, not leading zeros. toFixed(2) rounds and pads the fraction; Intl.NumberFormat adds commas and currency symbols:
const prices: number[] = [19.9, 5, 1250.456, 1.005];
for (const p of prices) {
console.log(String(p).padEnd(9), "toFixed:", p.toFixed(2));
}
const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
const fixed2 = new Intl.NumberFormat("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
console.log("currency:", usd.format(1250.456));
console.log("2 places:", fixed2.format(1250.4));
console.log("back to number:", Number((19.9).toFixed(2)) + 1);
Output:
19.9 toFixed: 19.90
5 toFixed: 5.00
1250.456 toFixed: 1250.46
1.005 toFixed: 1.00
currency: $1,250.46
2 places: 1,250.40
back to number: 20.9

toFixed(2) pads 5 to 5.00 and rounds 1.005 down; the currency formatter shows $1,250.46.The last price is stored a hair low in binary, so toFixed(2) rounds it down instead of up. I do money math in whole cents, and I validate input first with a check that a string is a number in TypeScript.
Which two-digit method should you use?
| Need | Use | Note |
|---|---|---|
| Pad 7 to “07” | String(n).padStart(2, "0") | Never truncates; negatives stay “-3” |
| Locale-aware padding | Intl.NumberFormat + minimumIntegerDigits | Add useGrouping: false |
| Month, day, hour, minute | Intl.DateTimeFormat with "2-digit" | Set timeZone |
| Two decimal places | toFixed(2) or maximumFractionDigits: 2 | Returns a string |
A few more TypeScript guides that pair well with this one:
- Get the current date in TypeScript
- Get the week number from a date in TypeScript
- Compare strings in TypeScript
- Convert dates to UTC in TypeScript
Frequently asked questions
How do I format a number as two digits in TypeScript?
Use String(n).padStart(2, "0"). It returns "07" for 7 and leaves 10 or more unchanged.
How do I add a leading zero to a single-digit month or day?
Pad getMonth() + 1 and getDate() with padStart(2, "0"), or use Intl.DateTimeFormat with month: "2-digit" and day: "2-digit".
Why does padStart not exist on type number?
It’s a string method. Convert first with String(n) or n.toString(), then call padStart.
How do I format negative numbers with two digits?
Intl.NumberFormat with minimumIntegerDigits: 2 keeps the sign in front, so -5 becomes “-05”. padStart returns “-5”.
How do I show two decimal places in TypeScript?
Call toFixed(2), which returns a string like “5.00”, or use Intl.NumberFormat with minimumFractionDigits and maximumFractionDigits set to 2.