# 3.8 數值(Number)

在 `javascript/practice` 資料夾下，建立 `number.html` 來練習。

## 數值宣告

```javascript
var x = 100;
var y = 3.14; // 浮點數

var z = "100"; // 這是字串 "100"，非數值。
```

要留意的是：數值不需要加雙引號或單引號，如果加上去的話，就會被視為字串。

## 指數

e = exponential (指數)：

```javascript
var x = 123e5;
var y = 123e-5;

console.log(x); // 12300000
console.log(y); // 0.00123
```

## 數值、字串相加

數值與數值相加，產生結果是數值；

數值與字串相加，會產生字串。

```javascript
var x = 10;
var y = 20;
var z = "20";

console.log(x + y); // 30
console.log(x + z); // "1020"
```

**隱含轉型**：上述的 `x + z`，其實真的執行的會是先將「數值 x 」轉成「字串 x」，這個過程就是 **隱含轉型**，是瀏覽器預設會幫我們執行的事。所以最後會變成字串與字串相加，就變成 "1020"。

## NaN、Infinity、-Infinity

NaN 是一個特殊的保留字，表示是「Not a Number」。

```javascript
var x = 100 / "其它";
var y = 2 / 0;
var z = -2 / 0;

console.log(x); // NaN
console.log(y); // Infinity
Console.log(z); // -Infinity
```

## 數值常用函式

### parseInt()

將字串數字轉成數值。例：

```javascript
var x = "100";
var y = 200;

console.log(x + y);           // 100200
console.log(parseInt(x) + y); // 300

var z = parseInt(x);
console.log(z);               // 100
console.log(typeof z);        // number
```

如果是浮點數(小數)，就會直接捨去小數部份：

```javascript
var y = 100.9;
console.log( parseInt(y) ); // 100
```

### parseFloat()

將字串數字轉成浮點數。如：

```javascript
var x = "100.9";
var y = parseFloat(x);

console.log(y);        // 100.9
console.log(typeof y); // number

console.log(x + 10); // 100.910
console.log(y + 10); // 110.9
```

### toString()

將數值轉成字串，例：

```javascript
var x = 100;
var y = x.toString();

console.log(x);        // 100
console.log(typeof x); // number

console.log(y);        // "100"
console.log(typeof y); // string
```

### toFixed()

指定四捨五入到小數點的第幾位，然後會回傳字串。例：

```javascript
var x = 100.526;
var y = x.toFixed(0);

console.log(y);        // 101
console.log(typeof y); // string

console.log( x.toFixed(1) ); // 100.5
console.log( x.toFixed(2) ); // 100.53
```

但其實有問題：(其實是[四捨六入五成雙](https://zh.wikipedia.org/wiki/%E5%A5%87%E9%80%B2%E5%81%B6%E6%8D%A8))

![](https://3126711450-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LokjetmQhrMQC7IVioJ%2F-MZOA7eqSsDdIFrUNSNV%2F-MZOBmbxQwOXwB5-MvIy%2Fto_fixed_explain.png?alt=media\&token=86c84c90-8413-43dc-a975-c7dc107e0c62)

示意圖：

![](https://3126711450-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LokjetmQhrMQC7IVioJ%2Fuploads%2F056H6DpD58JRTCzFbFOm%2Frounding.png?alt=media\&token=c9e555be-6b45-4a2c-9682-cd3f496ae84d)

```javascript
var x = 100.5555;

console.log( x.toFixed(1) ); // 100.6
console.log( x.toFixed(2) ); // 100.56
console.log( x.toFixed(3) ); // 100.555 (怪！)

var x = 100.5545;
console.log( x.toFixed(3) ); // 100.555 (怪！)

var x = 100.55551;
console.log( x.toFixed(3) ); // 100.556 (怪！)
```

### isNaN()

判斷某數數是不是「Not a Number」，即 NaN：

```javascript
var x = 100 / "其它";

console.log(x);        // NaN
console.log(isNaN(x)); // true
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.webmix.cc/javascript/ji-chu/3.8-shu-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
