Open In App

How to Parse Float with Two Decimal Places in TypeScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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);


Output:

123.46

Using Number constructor

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

Using bitwise OR operator

In this approach, we are using the bitwise OR operator which performs the bitwise OR operation between the individual bits of the two numbers. We are changing the integer values at the binary level using this approach.

Syntax:

result = operand1 | operand2;

Example: The below example uses bitwise OR operations to round the number 123.45678 to two decimal places, resulting in 123.45.

Javascript




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


Output:

123.45


Similar Reads

How to parse float with two decimal places in JavaScript ?
JavaScript is a client-side scripting language that's high-level, dynamically typed, and interpreted. It comes with a built-in method called parseFloat() which is used to convert a string into a floating-point number. We have different methods to parse float with two Decimal places in javascript, let us see the approaches one by one and understand
3 min read
How to Round Number to n Decimal Places in PHP ?
Rounding numbers to a specific decimal place is a common operation in programming, especially when dealing with precision in mathematical calculations. PHP provides several approaches to achieve this rounding. Table of Content Using round() FunctionUsing number_format() FunctionUsing sprintf() FunctionUsing round() FunctionThe round() function is a
2 min read
How to Round a Number to a Certain Number of Decimal Places in JavaScript ?
In JavaScript, you can round a number to a certain number of decimal places using various methods, such as toFixed(), Math.round(), Math.floor(), Math.ceil(), or by using the Number.prototype.toFixed() method. Here's how you can achieve it using these methods: Using toFixed() method:The toFixed() method returns a string representing the number roun
1 min read
How to parse JSON string in Typescript?
In this tutorial, we will learn how we can parse a JSON string in TypeScript. The main reason for learning about it is to learn how we can explicitly type the resulting string to a matching type. The JSON.parse() method will be used to parse the JSON string by passing the parsing string as a parameter to it. Syntax:JSON.parse(parsingString);We can
4 min read
Javascript Program to Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
4 min read
Javascript Program to Check if a string can be obtained by rotating another string 2 places
Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview Recommended: Please solve it on “PRACTICE ” f
2 min read
Sum of Digits at Even and Odd Places in PHP
Given a Number, the task is to sum the Digit of Even and Odd places using PHP. Calculating the sum of digits at even and odd places in a number is a common programming task. Here, we will cover all approaches with detailed explanations and code examples. Table of Content Using a LoopUsing Mathematical OperationsApproach 1: Sum of Digits at Even and
2 min read
Python | Parse a website with regex and urllib
Let's discuss the concept of parsing using python. In python we have lot of modules but for parsing we only need urllib and re i.e regular expression. By using both of these libraries we can fetch the data on web pages. Note that parsing of websites means that fetch the whole source code and that we want to search using a given url link, it will gi
2 min read
JavaScript JSON parse() Method
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object. Syntax: JSON.parse( string, function ) Parameters: This method accepts two parameters as mentioned above and described below: string: It is a required parameter and it contains a string that is written in JSON form
2 min read
Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API
The url.parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties. Syntax: url.parse( urlString, parseQueryString, slashesDenoteHost) Parameters: This method accepts three parameters as mentioned above and described below: urlString: It holds the URL string which needs to parse.parseQu
2 min read