How to use Number constructor In Typescript
In this approach, we are using the Number constructor to convert a numeric value to a string with a fixed number of decimal places using the toFixed method, then this constructor is used to the resulting string, which parses it back into a floating-point number with the specified precision.
Syntax:
Number(value: any): number;
Example: The below example uses the Number constructor and toFixed method to convert the number 123.45678 into a string with two decimal places and then parses it back to a floating-point number (123.46).
Javascript
const input: number = 123.45678; const output: number = Number(input.toFixed(2)); 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