javascript Number對象

來源:互聯網
上載者:User
文章目錄
  • Number 對象的方法
  • Number 對象的屬性

Javascript只有一個單一的數字類型,它在內部被表示為64位的浮點數,和Java的double一樣。不像大多數其他的程式設計語言,它沒有分離出整數類型,所以1與1.0是相同的值。這提供了很大的方便,因為它完全避免了短整數的溢出問題,你只要知道的一切就是它是一種數字。這樣就避免了一大堆因數字類型而導致的錯誤。

Number 對象的方法

FF: Firefox, IE: Internet Explorer

>
方法 描述 FF IE
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 對象的屬性

>
屬性 描述 FF IE
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())

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.