How to use the Math.ceil() method In Javascript
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.
Syntax:
Math.ceil(stringVariable);
Example: The below code ecplains the use of the Math.ceil method to convert string to a number.
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.ceil(str1), ", Type: ", typeof Math.ceil(str1));
console.log("str2: ", Math.ceil(str2), ", Type: ", typeof Math.ceil(str2));
Output
Before Conversion: str1: 233 , Type: string str2: 23.33 , Type: string After Conversion: str1: 233 , Type: number str2: 24 , 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
Contact Us