I need to convert a floating number to a string array, for example var num=25.67 into var str_array[0] = '2', str_array[1]='5', str_array[2]='.', str[3]='6'; str_array[4]='7' .
Please show your code. Thank you very in advance.
2 Answers
It's easy. You just have to convert the num var into an string (with the .toString() method) and then .split('') it (the '' parameter makes it split every character).
var num = 25.67;
var str_array = num.toString().split('');
You can also use this code:
var num = 25.67;
var str_array = num.toString();
Then str_array will be no more an array, but a string (which can be treated as an array)
3 Comments
Danilo Valente
What I mean is that you can access a character from a string using square brackets like you do when you wanna access an item of an array
user1456767
Thank you. Really aprreciate your help.