1. Using parseInt() function:
The parseInt() function parses a string argument and returns an integer.
Example 1: String to int
console.log(parseInt('100'));

Example 2: String to int with base 2
console.log(parseInt('200', 2));
Example 3: White-spaces are ignored.
console.log(parseInt(' 300 '));
Example 4: Leading zero's are ignored.
console.log(parseInt('000400'));
Example 5: Decimal's are ignored.
console.log(parseInt('500.678'));
If the String value provided is not a valid number, then we get NaN as an output.
Example 6: NaN output for Invalid String Number.
console.log(parseInt('hello'));
Summary: parseInt()
- It accepts an optional second argument (radix) that specifies the base of the number.
- Leading whitespace is ignored.
- Leading zeros are ignored.
- Decimals are ignored..
- If the string starts with '0x' or '0X', it's interpreted as a hexadecimal number.
- If the string doesn't start with a valid numeric character, parseInt() returns NaN.
- If the radix is not specified or is 0, JavaScript assumes base 10.
- If the radix is not in the range 2-36 or is not a number, parseInt() returns NaN.
- Negative numbers can be parsed by including a '-' sign at the beginning.
- If the string contains non-numeric characters after valid numeric characters, parseInt() stops parsing at the non-numeric character.
- If the string is empty or contains only non-numeric characters, parseInt() returns NaN
2. Using parseFloat() function
The parseFloat() function parses a string argument and returns a floating point number.
Example 7:
console.log(parseFloat('600'));

Example 8:
console.log(parseFloat('700.89'));
Example 9:
console.log(parseFloat(' 800.1 '));
Example 10:
console.log(parseFloat('000900.1'));
Example 11:
console.log(parseFloat('Code2care'));
3. Using Number() Constructor
Example 12:
console.log(Number('1200'));

4. Using + Operator
Example 13:
console.log(+'1300');

5. Math.floor() function (truncates decimal)
Example 14:
console.log(Math.floor('1400.2'));

6. Math.round() function (rounds up/down decimals)
Example 15:
console.log(Math.round('1500.7'));
console.log(Math.round('1500.2'));

7. Math.ceil() function (rounds up decimals)
Example 16:
console.log(Math.ceil('1600.2'));

8. BigInt() Constructor
Example 17:
console.log(String(BigInt('238923820340230470237')));
9. Bitwise OR | Operator
Example 18:
console.log("1800" | 0);
10. Implicit Type Conversion
Example 19:
When we do arithmetic operation with a String and a Number, JavaScript automatically converts strings into a number.
console.log("19"*100);

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to JavaScript,
- Get Device Screen Width and Height using javaScript
- How to Print from JavaScript HTML using window.print
- Send Extra Data with Ajax Get or Post Request
- How to get UTC (GMT) time using JavaScript
- How to detect Browser and Operating System Name and Version using JavaScript
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React
- How to send email from JavaScript HTML using mailto
- Remove items from JavaScript array
- JavaScript : Get current page address
- Add Animated Scrolling to Html Page Title Script
- [javaScript] Convert text case to lowercase
- JavaScript date in yyyy-MM-dd format
- Power of Print Statements in JavaScript: A Comprehensive Guide
- Submit html form on dropdown menu value selection or change using javascript
- Write javaScript code in Swedish using FikaScript
- Javascript convert text case from uppercase to lowercase
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- JavaScript: Count Words in a String
- Meaning of javascript:void(0) explained with example
- JavaScript: Generate Random Numbers between 1 and 3
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to get current URL Location using Javascript HTML
- Fix: SyntaxError: The requested module does not provide an export named default
- Fix: Error: Cannot find module /node-examples/init
- JavaScript: Check if variable is a number
More Posts:
- Pdf Text to Speech option in Mac OS X Preview App - Mac-OS-X
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console - Java
- ls command to list only directories - Linux
- How to hide or disable iOS 14 App Library on iPhone? - Apple
- How to Change Background/Foreground Color to Vim - Linux
- How to Show Commit Date in Eclipse Git History Details - Git
- Setting and Updating AWS CLI Configuration - AWS
- Spring Boot + Redis Cloud Configuration and Setup Tutorial - Java