Converting String to Numbers


Converting String to Numbers

If you have a string and want to convert it to a number then use the parseInt() method. For example this script returns integer values containing 1234 and 43 respectively into X:

X = parseInt("1234",10);

X = parseInt("34abc",10);


The second argument (10) specifies you want to use a base 10 numbering system. It's unusual to use anything for this parameter except 10 and you should always specify it as the default is a bit unpredictable. (See, for example, if you are interested as to why)

If you need to have decimals then use parseFloat(). For example this script returns floating point values 1234.345 and 34.7 respectively into X:    

X = parseFloat("1234.345");

X = parseFloat("34.7abc");


Remember that these are floating point values so they are not always as accurate or as predictable as signed or packed decimals numbers.