Have you ever found yourself needing to display numbers as two digits in your TypeScript projects, like “07” instead of “7”? In this tutorial, I’ll walk you through several ways to format numbers as two digits in TypeScript. We’ll cover built-in JavaScript methods, string manipulation, and formatting with Intl.NumberFormat.
Here are the methods:
Method 1: Using padStart() for String Padding
My go-to method for ensuring two-digit formatting is the padStart() string method in TypeScript. It’s simple, reliable, and works in all modern browsers and Node.js environments.
Here is an example:
function formatTwoDigits(num: number): string {
return num.toString().padStart(2, '0');
}
// Example: Formatting a month for US-style dates (MM/DD/YYYY)
const month = 7;
const formattedMonth = formatTwoDigits(month); // "07"
console.log(`Today's date: ${formattedMonth}/21/2025`);
It’s concise and doesn’t require external libraries. Just convert the number to a string and pad it with a leading zero if needed.
I executed the above TypeScript code using VS Code, and you can see the exact output in the screenshot below:

Check out Get String Between 2 Characters In TypeScript
Method 2: Using Intl.NumberFormat for Locale-Aware Formatting
For projects that may need to scale internationally or require more advanced formatting, I recommend Intl.NumberFormat. It’s built into JavaScript and TypeScript and handles padding with options.
Let me show you an example.
const twoDigitFormatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
});
const day = 4;
const formattedDay = twoDigitFormatter.format(day); // "04"
console.log(`Event Date: 07/${formattedDay}/2025`);
If you’re already using internationalization features, or want to future-proof your code for different locales, this method is ideal. It’s especially handy for USA-focused apps that might expand globally.
You can see the exact output in the screenshot below:

Read Convert Typescript Array to String With Separator
Method 3: Manual String Concatenation
Sometimes, you just want the simplest possible solution. Here’s how you can manually prepend a zero:
function formatTwoDigitsManual(num: number): string {
return num < 10 ? '0' + num : String(num);
}
// Example: Formatting an order number
const orderNumber = 5;
console.log(`Order ID: #${formatTwoDigitsManual(orderNumber)}`); // "Order ID: #05"
It’s quick and easy, especially for small scripts or when you want to avoid method calls.
Here is the exact output in the screenshot below:

Check out How to Check If Object Is Empty in TypeScript?
Method 4: Using Template Literals
Template literals can also be used for efficient formatting. Here is an example.
function formatWithTemplate(num: number): string {
return `${num < 10 ? '0' : ''}${num}`;
}
const minute = 8;
console.log(`Time: 09:${formatWithTemplate(minute)} AM`); // "Time: 09:08 AM"
Tip:
This method is readable and integrates well with other string formatting.
Check out How to Convert Number to Boolean in TypeScript
TypeScript number format 2 digits examples
Now, let me show you two examples of a number format with two digits in TypeScript.
Let’s look at a few practical scenarios where two-digit formatting is important in US-based apps:
1. Formatting Dates for MM/DD/YYYY
Here is an example of formatting dates for a MM/DD/YYYY format in TypeScript.
function formatDate(month: number, day: number, year: number): string {
return `${formatTwoDigits(month)}/${formatTwoDigits(day)}/${year}`;
}
console.log(formatDate(7, 4, 2025)); // "07/04/2025" (Independence Day!)
2. Generating Invoice or Order Numbers
Here is an example of generating invoice or order numbers to two digits in TypeScript.
function formatOrderNumber(order: number): string {
return `INV-${formatTwoDigits(order)}`;
}
console.log(formatOrderNumber(3)); // "INV-03"
3. Displaying Time on Digital Clocks
Here is another TypeScript number format example with two digits.
function formatTime(hour: number, minute: number): string {
return `${formatTwoDigits(hour)}:${formatTwoDigits(minute)}`;
}
console.log(formatTime(9, 5)); // "09:05"
In this tutorial, I will explain how to format numbers as two digits in TypeScript using various methods. But I recommend using the padStart() method. If you’re working with internationalization or want more formatting options, try Intl.NumberFormat.
You may also like the following tutorials:
- Convert String To Double In TypeScript
- Get String After Character In TypeScript
- TypeScript Check If String Is Number
- How to Format Numbers with Commas in TypeScript

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.