Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

string to number javascript shortcut

1. Using parseInt()
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned.
This method has a limitation though. If you parse the decimal number, it will be rounded off to the nearest integer value and that value is converted to string. One might need to use parseFloat() method for literal conversion.
myString = '129' 
console.log(parseInt(myString)) // expected result: 129

a = 12.22
console.log(parseInt(a)) // expected result: 12
2. Using Number()
Number() can be used to convert JavaScript variables to numbers. We can use it to convert the string too number.
If the value cannot be converted to a number, NaN is returned.
Number("10");          // returns 10
Number(" 10  ");       // returns 10
Number("10.33");       // returns 10.33
3. Using Unary Operator (+)
The unary plus operator (+) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
const x = 25;
const y = -25;
console.log(+x); // expected output: 25
console.log(+y); // expected output: -25
console.log(+''); // expected output: 0
4. Using parseFloat()
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.
parseFloat("10");        // returns 10
parseFloat("10.33");     // returns 10.33
parseFloat("10 20 30");  // returns 10
parseFloat("10 years");  // returns 10
parseFloat("years 10");  // returns NaN
5. Using Math.floor()
The Math.floor() function returns the largest integer less than or equal to a given number. This can be little tricky with decimal numbers since it will return the value of the nearest integer as Number.
str = '1222'
console.log(Math.floor(str)) // returns 1222

a = 12.22
Math.floor(a) // expected result: 12
6. Multiply with number
Multiplying the string value with the 1 which won’t change the value and also it will be converted to number by default.
str = '2344'
console.log(str * 1) // expected result: 2344
7. Double tilde (~~) Operator
We can use the double tilde operator to convert the string to number.
str = '1234'
console.log(~~str) // expected result: 1234
negStr = '-234'
console.log(~~negStr) // expected result: -234
Comment

PREVIOUS NEXT
Code Example
Javascript :: create sub array from array with values that pass condition javascript 
Javascript :: making a react js website project ready for hosting 
Javascript :: speed of sound 
Javascript :: change to a constant variable 
Javascript :: JS call url many times 
Javascript :: Nodemailer Reuseable Code 
Javascript :: take money from user and give change as output using javascript 
Javascript :: javascript stopped working 
Javascript :: where to make the hooks functions 
Javascript :: service erstellen angular 
Javascript :: microseconds to current time js code 
Javascript :: node get request filepath 
Javascript :: what is the equivalent of cascade on delete in mongoose 
Javascript :: unable to append div multiple times 
Javascript :: my code agly because alot of if and else dev community 
Javascript :: json serializable snake case 
Javascript :: how to get the folder of the extension 
Javascript :: typeorm caching queries time limit globally 
Javascript :: on number copy pase formate in reactjs 
Javascript :: url.createobjecturl 
Javascript :: "date change error" 
Javascript :: Block Alignment Toolbar Using ES5 in Wordpress 
Javascript :: used as a function, Number() will convert another value 
Javascript :: Node Red to their W1HQ station 
Javascript :: reset default style javascript 
Javascript :: react hooks remove item from array 
Javascript :: jshack130mhrl 
Javascript :: why in the hell does JavaScript - Date getMonth() return 11 
Javascript :: react avoid spreading non-dom props across component 
Javascript :: get call log in react native with filter android 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =