Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to number node.js

I'm trying to convert req.params to Number because that is what I defined in my schema for year param.

I have tried

req.params.year = parseInt( req.params.year, 10 );   

and

Number( req.params.year); 

and

1*req.params.year; 

but non of them works. Do I need to install something?

like image 426
user3488862 Avatar asked May 17 '16 08:05

user3488862


People also ask

How do I convert a string to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.

How do I convert a string to a number in JavaScript?

How to convert a string to a number in JavaScript using the unary plus operator ( + ) The unary plus operator ( + ) will convert a string into a number. The operator will go before the operand. We can also use the unary plus operator ( + ) to convert a string into a floating point number.

How do I convert a string to a number in react?

How do I convert a string to a number in react? We can make use of the Number object to convert a string to a number. const amount = Number(inputValue); const convenienceFee = Number('20'); const inputValue : string = "250" const amount : number = Number(inputValue);

How do you use parseFloat?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.


2 Answers

You do not have to install something.

parseInt(req.params.year, 10); 

should work properly.

console.log(typeof parseInt(req.params.year)); // returns 'number' 

What is your output, if you use parseInt? is it still a string?

like image 101
Tusk Avatar answered Oct 07 '22 02:10

Tusk


Using parseInt() is a bad idea mainly because it never fails. Also because some results can be unexpected, like in the case of INFINITY.
Below is the function for handling unexpected behaviour.

function cleanInt(x) {     x = Number(x);     return x >= 0 ? Math.floor(x) : Math.ceil(x); } 

See results of below test cases.

console.log("CleanInt: ", cleanInt('xyz'), " ParseInt: ", parseInt('xyz')); console.log("CleanInt: ", cleanInt('123abc'), " ParseInt: ", parseInt('123abc')); console.log("CleanInt: ", cleanInt('234'), " ParseInt: ", parseInt('234')); console.log("CleanInt: ", cleanInt('-679'), " ParseInt: ", parseInt('-679')); console.log("CleanInt: ", cleanInt('897.0998'), " ParseInt: ", parseInt('897.0998')); console.log("CleanInt: ", cleanInt('Infinity'), " ParseInt: ", parseInt('Infinity')); 

result:

CleanInt:  NaN  ParseInt:  NaN CleanInt:  NaN  ParseInt:  123 CleanInt:  234  ParseInt:  234 CleanInt:  -679  ParseInt:  -679 CleanInt:  897  ParseInt:  897 CleanInt:  Infinity  ParseInt:  NaN 
like image 27
tejp124 Avatar answered Oct 07 '22 02:10

tejp124