views:

63

answers:

3

Hi all,

how can I split a number into sub digits. I have a number 20:55 n need to split in to two parts as 20 and 55.

var mynumber = 20:55;
var split = mynumber.toString();
document.write("Hour : " +  split[0]);
document.write("Minutes : " +  split[1]);

but am getting wrong value after toString() function call.

EDIT: 1 This is what I want. This is working fine but only works with windows. In linux Firefox am not getting the correct value in function .. something like 12.200000000000000001 instead of 12.25

<script type="text/javascript">
var myarray = new Array();
myarray =   [[12.25, 1],[13.25,1]];
//alert(myarray[0][0]);
func(myarray[0][0]) ;
function func(n) {
    var split = n.split(".");
    document.writeln("Number  : " +  n); // Number  : 12.25
    document.writeln("Hour : " +  split[0]); // Hour : 12
    document.writeln("Minutes : " +  split[1]); // Minutes : 25
}
</script>
A: 

Use split function

var numSplit = mynumber.split(':');

Then you can access each value using numSplit[0] and numSplit[1]

rahul
+1  A: 

Use split() function

var number_array=mynumber.split(":")

On your example

var mynumber = "20:55";
var split = mynumber.split(":");
document.write("Hour : " +  split[0]);
document.write("Minutes : " +  split[1]);

More on this

http://www.w3schools.com/jsref/jsref_split.asp

hgulyan
But mynumber is not a string, its came from an array.. that is why I tried to convert that into string for splitting.
coderex
Then show us how do you get your number 20:55? (It's not a number really) I'm not sure how do you get it, but you can try mynumber.toString().split(":")
hgulyan
+1  A: 

You are using split(), a string function, on a number. Use this:

    var split = n.toString().split(".");

Works perfectly on Firefox 3.6.3 (and Opera 10.60, and Chromium 5), Ubuntu Linux 10.04.

Piskvor
actually it's ":", not "."
hgulyan
@hgulyan: see the question, second code block, under "This is what I want" - that one is using `.`
Piskvor
Sorry, didn't see that question was edited.
hgulyan