How to Round to 2 Decimals in TypeScript?

Recently, I was required to round numbers to two decimal places in TypeScript. This helps to display numbers correctly, and it is easy to do in TypeScript.

In this tutorial, I’ll walk you through several easy ways to round to 2 decimals in TypeScript with practical examples.

TypeScript Round to 2 Decimals

There are many scenarios, such as prices, tax rates, and currency values, that you need to show with two decimal places. If you’re working with financial data, invoices, or scientific measurements, rounding to two decimals helps provide accuracy and professionalism.

Here are a few methods to round to 2 decimal places in TypeScript.

Method 1: Using toFixed()

The simplest and most popular way to round numbers to two decimal places in TypeScript is with the built-in toFixed() method.

The toFixed() method returns a string representing the number in fixed-point notation. Here’s how I use it:

const price: number = 19.9876;
const roundedPrice: string = price.toFixed(2);
console.log(roundedPrice); // Output: "19.99";

Here is the exact output in the screenshot below:

typescript round to 2 decimals

Note:
toFixed() returns a string, not a number. If you need the result as a number (for further calculations), you can convert it:

const roundedNumber: number = parseFloat(price.toFixed(2));
console.log(roundedNumber); // Output: 19.99

Here is a real example. Let’s say you’re displaying the average gas price in Texas:

const avgGasPrice: number = 3.45789;
console.log(`Average gas price: $${avgGasPrice.toFixed(2)}`);
// Output: Average gas price: $3.46

Check out Typescript Split String [With Examples]

Method 2: Using Math Functions (Math.round)

If you prefer to keep the result as a number, you can use Math.round() with a little math trick.

Multiply the number by 100, round it, and then divide by 100:

const value: number = 5.6789;
const rounded: number = Math.round(value * 100) / 100;
console.log(rounded); // Output: 5.68

This method is handy when you need the result as a number and want to avoid converting between types.

You can see the exact output in the screenshot below:

Round to 2 Decimals in TypeScript

Example: Calculate Sales Tax

Imagine you’re calculating sales tax for a purchase, and you can do this by using the below TypeScript code:

const subtotal: number = 47.256;
const taxRate: number = 0.07; // 7%
const tax: number = Math.round(subtotal * taxRate * 100) / 100;
console.log(`Sales Tax: $${tax}`);
// Output: Sales Tax: $3.31

Read Merge Enums in TypeScript with Different Values

Method 3: Custom Utility Function

If you are working on a large project, then it is better to create a reusable utility function in TypeScript.

This makes my code cleaner and more maintainable.

function roundToDecimals(num: number, decimals: number = 2): number {
  const factor = Math.pow(10, decimals);
  return Math.round(num * factor) / factor;
}

// Usage
const housePrice: number = 325000.987;
console.log(roundToDecimals(housePrice, 2)); // Output: 325000.99

Why This Method?

  • Reusable: Works for any number of decimals.
  • Consistent: Keeps your codebase DRY (Don’t Repeat Yourself).

Read Convert Typescript Dictionary to String

Method 4: Round with Number.EPSILON

If you are working with floating-point precision issues (common in financial or scientific applications), I recommend using Number.EPSILON in TypeScript.

function roundToTwo(num: number): number {
  return Math.round((num + Number.EPSILON) * 100) / 100;
}

const tempInChicago: number = 72.34567;
console.log(roundToTwo(tempInChicago)); // Output: 72.35

If you’re running into strange rounding errors (like 1.005 rounding to 1.00 instead of 1.01), this method helps avoid those quirks.

You can see the exact output in the screenshot below:

typescript round number to 2 decimal places

Read How to Check the Type of a Variable in TypeScript

Formatting for Display: Using Internationalization (Intl.NumberFormat)

If you’re displaying currency or percentages in a user interface, use the Intl.NumberFormat API for proper formatting according to U.S. standards.

const amount: number = 1234.567;

const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
});

console.log(formatter.format(amount)); // Output: $1,234.57

Why I Love This

  • Locale-aware: Automatically uses commas, decimals, and currency symbols correctly for the U.S.
  • Flexible: Easily adapts for different locales if your app grows internationally.

Conclusion

In this tutorial, I explained how to round numbers to two decimal places in TypeScript using several methods, such as using toFixed(), Math.round(), or a custom utility function.

Remember to choose the approach that best fits your use case:

  • Use toFixed() for quick display.
  • Use Math.round() for calculations.
  • Use Intl.NumberFormat for polished, locale-aware formatting.

I hope you found this guide helpful! If you have any questions or want to share your own rounding tips, feel free to leave a comment below.

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