How to use the Math.floor() method In Javascript

The Math.floor() method will return the low value if the floating point string is passed to it and convert its type from string to number.

Syntax:

Math.floor(stringVariable);

Example: The below code illustrates the use of Math.floo() method to convert a string into a number.

Javascript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.floor(str1), ", Type: ", typeof Math.floor(str1));
console.log("str2: ", Math.floor(str2), ", Type: ", typeof Math.floor(str2));

Output
Before Conversion: 
str1:  233 , Type:  string
str2:  23.33 , Type:  string
After Conversion: 
str1:  233 , Type:  number
str2:  23 , Type:  number

Convert a String to Number in JavaScript

Converting strings to numbers in JavaScript is a fundamental operation, crucial for various tasks like handling user inputs or data processing. By converting strings to numeric values, JavaScript enables developers to perform mathematical operations and comparisons accurately, enhancing the versatility and functionality of their applications.

Table of Content

  • Using the ‘+’ operator
  • Using the Number() constructor
  • Using the parseInt() method
  • Using the parseFloat() method
  • Using the Math.floor() method
  • Using the Math.ceil() method
  • Using the Math.round() method

Similar Reads

Using the ‘+’ operator

The + operator can be operated with the string by using it before the name of the string variable to convert it into a number....

Using the Number() constructor

The Number() constructor can be used to convert a string into a number by passing the string value as an argument to it....

Using the parseInt() method

The parseInt() method can be used to convert a numeric string into a number by passing it as an arguement to it. It will always return an integer either you pass a floating point string or integer string....

Using the parseFloat() method

The parseFloat() method can also be used to convert a string into a number in the same way we use the parseInt() method by passing the string value to it. It will return the value as it is as passed to it....

Using the Math.floor() method

The Math.floor() method will return the low value if the floating point string is passed to it and convert its type from string to number....

Using the Math.ceil() method

The Math.ceil() method can be used in the same way as the Math.floor() method was used. It will convert the string type from string to number and returns the upper value if floating point string passed to it....

Using the Math.round() method

The Math.rond() method will round up the passed floating string and returns its value after rounding it up. It also converts the type of string to number....

Contact Us