How do I convert a string into an integer in JavaScript? -


how convert string integer in javascript?

the simplest way use native number function:

var x = number("1000") 

if doesn't work you, there parseint, unary plus, parsefloat floor, , math.round methods.

parseint:

var x = parseint("1000", 10); // want use radix 10     // decimal number leading 0 , old browser ([ie8, firefox 20, chrome 22 , older][1]) 

unary plus if string in form of integer:

var x = +"1000"; 

if string or might float , want integer:

var x = math.floor("1000.01"); //floor automatically converts string number 

or, if you're going using math.floor several times:

var floor = math.floor; var x = floor("1000.01"); 

if you're type forgets put radix in when call parseint, can use parsefloat , round like. here use floor.

var floor = math.floor; var x = floor(parsefloat("1000.01")); 

interestingly, math.round (like math.floor) string number conversion, if want number rounded (or if have integer in string), great way, maybe favorite:

var round = math.round; var x = round("1000"); //equivalent round("1000",0) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -