---
title: "JavaScript 笔记 8 - Number对象"
author: "rcatws"
date: "2016-02-17T02:41:29Z"
tags: []
categories: ["Web前端"]
slug: "javascript-8-number"
draft: false
lastmod: "2016-02-17T02:41:29Z"
id: 2276808
url: "https://rcatws.iteye.com/blog/2276808"
---

**JavaScript Number对象**  
Number 对象是原始数值的包装对象。

JavaScript 只有一种数字类型。

可以使用也可以不使用小数点来书写数字。

所有 JavaScript 数字均为 64 位。

 

**精度**

整数（不使用小数点或指数计数法）最多为 15 位。  
小数的最大位数是 17，但是浮点运算并不总是 100% 准确。  
能表示最大值为±1.7976931348623157 x 10308，最小值为±5 x 10 -324。

**数字可以是数字或者对象**  
数字可以私有数据进行初始化，就像 x = 123;  
JavaScript 数字对象初始化数据， var y = new Number(123);

**对象属性**

属性 描述 constructor 返回对创建此对象的Number函数的引用。 prototype 允许你有能力向对象添加属性和方法。 MAX\_VALUE 可表示的最大数。 MIN\_VALUE 可表示的最小数。 NEGATIVE\_INFINITY 负无穷大，溢出时返回该值。 NaN 非数字值。 POSITIVE\_INFINITY 正无穷大，溢出时返回该值。

 

**对象方法**

方法 描述

toExponential()

把对象的值转换为指数计数法。 toFixed() 把数字转换为字符串，结果的小数点后有指定的位数的数字。 toPrecision() 把数字格式为指定的长度 toString() 把数字转换为字符串，使用指定的基数。 valueOf() 返回一个Number对象有的基本数据值。

 

**测试代码**

```
var num1 = 5.123456789;
var num2 = 98765.4321;
var num3 = 98.7654321;
var num4 = 987654321;
var num5 = 100;

document.write('<h3>' + 'num1 = ' + num1 + '</h3>');
document.write('<h3>' + 'num2 = ' + num2 + '</h3>');
document.write('<h3>' + 'num3 = ' + num3 + '</h3>');
document.write('<h3>' + 'num4 = ' + num4 + '</h3>');
document.write('<h3>' + 'num5 = ' + num5 + '</h3>');

//函数toExponential([x]), 可把对象的值转换成指数计数法, x = 0 - 20。
document.write('<p>' + 'num1.toExponential(3) = ' + num1.toExponential(3) + '<p>');
document.write('<p>' + 'num1.toExponential(15) = ' + num1.toExponential(15) + '<p>');	

//函数toFixed(x),可把 Number 四舍五入为指定小数位数的数字, x = 0 - 20。
document.write('<p>' + 'num1.toFixed(3) = ' + num1.toFixed(3) + '</p>');
document.write('<p>' + 'num1.toFixed(5) = ' + num1.toFixed(5) + '</p>');	
document.write('<p>' + 'num1.toFixed(15) = ' + num1.toFixed(15) + '</p>');	

//函数toPrecision(x),可在对象的值超出指定位数时将其转换为指数计数法, x = 1 - 21。
document.write('<p>' + 'num2.toPrecision(6) = ' + num2.toPrecision(6) + '</p>');
document.write('<p>' + 'num3.toPrecision(6) = ' + num3.toPrecision(6) + '</p>');
document.write('<p>' + 'num4.toPrecision(6) = ' + num4.toPrecision(6) + '</p>');

document.write('<p>' + 'num2.toPrecision() = ' + num2.toPrecision() + '</p>');

document.write('<p>' + 'num2.toPrecision(21) = ' + num2.toPrecision(21) + '</p>');
document.write('<p>' + 'num2.toPrecision(20) = ' + num2.toPrecision(20) + '</p>');
document.write('<p>' + 'num4.toPrecision(21) = ' + num4.toPrecision(21) + '</p>');

//函数toString([radix]),把数字转换成字符串, radix = 2 - 36。
document.write('<p>' + 'num5.toString() = ' + num5.toString() + '</p>');
document.write('<p>' + 'num5.toString(2) = ' + num5.toString(2) + '</p>');
document.write('<p>' + 'num5.toString(8) = ' + num5.toString(8) + '</p>');
document.write('<p>' + 'num5.toString(16) = ' + num5.toString(16) + '</p>');
document.write('<p>' + 'num5.toString(36) = ' + num5.toString(36) + '</p>');
```

 

**结束整理**

- 整数（不使用小数点或指数计数法）最多为 15 位。
- 小数的最大位数是 17，但是浮点运算并不总是 100% 准确。
- 前缀为0，则把数值常量解释为八进制，前缀为0x，则解释为十六进制。Number对象的属性，除了继承的constructor和prototype外，其他的都只能通过Number自身来引用，创建的Number对象的属性无意义。

**toPrecistion()**

- 指定长度小于数字原长度, 有小数位先去除, 无小数位后变成指数计数。
- 指定长度大于数字原长度, 补小数位, 原数字为整数补位后无偏差，原数字为有小数时，由于是浮点运算并不总是100%准确。