How to use toFixed method In Typescript

In this approach, the toFixed method is used to mainly format the input number which is the specified number of digits after the decimal place.

Syntax:

number.toFixed(digits?: number): string;

Example: The below example parses the float number to two decimal places using the toFixed method, where we have given the 2 as the argument which represents the number of decimal places.

Javascript




const input: number = 123.45678;
const output: number = ((input * 100) | 0) / 100;
console.log(output);


Output:

123.46

How to Parse Float with Two Decimal Places in TypeScript ?

In Typescript, parsing float with two decimal places refers to mainly converting the float number with two digits after the decimal points. There are various approaches to parse float with two decimal places in TypeScript which are as follows:

Table of Content

  • Using Math.round method
  • Using toFixed method
  • Using Number constructor
  • Using bitwise OR Operator

Similar Reads

Using Math.round method

In this approach, we are using the Math.round method which is similar to the JavaScript function that rounds the number to the nearest integer. This function takes the floating number as the input and it returns the nearest integer....

Using toFixed method

...

Using Number constructor

In this approach, the toFixed method is used to mainly format the input number which is the specified number of digits after the decimal place....

Using bitwise OR operator

...