How to Parse Float with Two Decimal Places in TypeScript ?
Last Updated :
24 Apr, 2025
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:
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.
Syntax:
Math.round(x: number): number;
Example: To demonstrate how the Math.round method parses the float number with two decimal places. The 123.45678 input number is been parsed with two decimal places (123.46).
JavaScript
const input: number = 123.45678;
const output: number = Math.round(input * 100) / 100;
console.log(output);
Output:
123.46
Using toFixed method
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);