1

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 2

3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

"Treated as an array" - No, a string does not inherit methods from Array.prototype. Though it does have numeric keys which act like .charAt().
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
Thank you. Really aprreciate your help.
3

Try with

var digits = num.toString().split('');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.