Ruby's Louvre

每天学习一点点算法

导航

javascript Number对象

Javascript只有一个单一的数字类型,它在内部被表示为64位的浮点数,和Java的double一样。不像大多数其他的编程语言,它没有分离出整数类型,所以1与1.0是相同的值。这提供了很大的方便,因为它完全避免了短整数的溢出问题,你只要知道的一切就是它是一种数字。这样就避免了一大堆因数字类型而导致的错误。

Number 对象的方法

FF: Firefox, IE: Internet Explorer

方法描述FFIE
toString 把数字转换为字符串,使用指定的基数。 1.0 4.0
toLocaleString 把数字转换为字符串,使用本地数字格式顺序。 1.0 4.0
toFixed 把数字转换为字符串,结果的小数点后有指定位数的数字。 1.0 5.5
toExponential 把数字转换为字符串,结果采用指数计数法,小数点后有指定位数的小数。 1.0 5.5
toPrecision 把数字转换为字符串,结果中包含指定位数的有效数字。采用指数计数法或定点计数法,由数字的大小和指定的有效数字位数决定采用哪种方法。 1.0 5.5
toSource() 代表对象的源代码 1.0 -
valueOf 返回一个 Number 对象的基本数字值。 1.0 4.0

Number 对象的属性

属性描述FFIE
MAX_VALUE 可表示的最大的数。 1.0 4.0
MIN_VALUE 可表示的最小的数。 1.0 4.0
NaN 非数字值。 1.0 4.0
NEGATIVE_INFINITY 负无穷大,溢出时返回该值。 1.0 4.0
POSITIVE_INFINITY 正无穷大,溢出时返回该值。 1.0 4.0
var isNumber = function(n){
    return typeof n === 'number' && isFinite(n);
}
alert((0.1+0.2) == 0.3)//false

获得随机数

function getRandom(min,max) {
    return Math.floor(Math.random()*(max-min+1))+min;
}

There is a special value in JavaScript called Infinity. It represents a number too big for JavaScript to handle. Infinity is indeed a number, as typing typeof Infinity in the console will confrm. You can also quickly check that a number with 308 zeros is ok, but 309 zeros is too much. To be precise, the biggest number JavaScript can handle is 1.7976931348623157e+308 while the smallest is 5e-324.

javascript十进制转二进制,二进制转十进制

var random = (function() {
    var seed = 49734321;
    return function() {
        // Robert Jenkins' 32 bit integer hash function.
        seed = ((seed + 0x7ed55d16) + (seed << 12))  & 0xffffffff;
        seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
        seed = ((seed + 0x165667b1) + (seed << 5))   & 0xffffffff;
        seed = ((seed + 0xd3a2646c) ^ (seed << 9))   & 0xffffffff;
        seed = ((seed + 0xfd7046c5) + (seed << 3))   & 0xffffffff;
        seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
        return (seed & 0xfffffff) / 0x10000000;
    };
})();
  
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())
console.log(random())

posted on 2009-09-30 10:35  司徒正美  阅读(1806)  评论(0编辑  收藏  举报