开发者

how to split a number in javascript Linux Firefox getting incorrect value, windows show the correct value

开发者 https://www.devze.com 2023-01-06 11:25 出处:网络
how can I split a number into sub digits. I have anumber 20:55 n need to split in to two parts as 20 and 55.

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 =开发者_StackOverflow n.split(".");
    document.writeln("Number  : " +  n); // Number  : 12.25
    document.writeln("Hour : " +  split[0]); // Hour : 12
    document.writeln("Minutes : " +  split[1]); // Minutes : 25
}
</script>


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


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.


Use split function

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号