Ad

Which Javascript Function Can Be Used To Covert An Array Of Characters Into A Number?

- 1 answer

There are a few Javascript functions available to convert anything into its equivalent number. Number() operates on an Object, valueOf(), parseFloat, parseInt() are also available.

I have an array which stores numbers 0-9 and decimal point, the elements of the array taken together represents a number. What is the best way to convert this array into a number, whole or fractional?

EDIT: Apologies if I were not clear before. The array, holding the 0-9 characters and possibly a decimal point, could represent either a whole number(without the decimal obviously) or a fractional number. So please suggest something that works for both cases. Thanks.

Ad

Answer

Try this

var a = [1,2,3,".",2,3];
var num = +a.join("");
Ad
source: stackoverflow.com
Ad