How to Format Numbers as Two Digits in TypeScript (With Examples)

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:

Format Numbers as Two Digits in TypeScript

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:

typescript number format 2 digits

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:

typescript number format 2 digits example

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:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App