# <font class="h2">四捨五入、無條件捨去、無條件進位</font>
###### tags: `javascript`
<style>
.h2 {
background: linear-gradient(135deg,#fff,#537479) ;
color: #537479;
display:block;
padding: 6px 5px;
border-radius: 4px;
}
.h3 {
background: linear-gradient(180deg,#fff 50%,#c9d5d4) ;
color: #537479;
display:block;
padding: 6px 5px;
border-bottom: 3px solid #537479;
}
.h4 {
color: #537479;
font-weight:bold;
font-size:1.1em;
}
</style>
<br>
### <font class="h4">➤Math.round() 四捨五入</font>
```javascript
Math.round(3.14) // 3
Math.round(5.49999) // 5
Math.round(5.5) // 6
Math.round("5.50001") // 6
Math.round(-5.49999) // -5
Math.round(-5.5) // -5
Math.round(-5.50001) // -6
```
<br>
優先序`()` > `Math.round` > `/`
```javascript
Math.round(18.62645 * 10) / 10 // 18.6
Math.round(18.62645 * 100) / 100 // 18.63
Math.round(18.62645 * 1000) / 1000 // 18.626
```
<br>
:::info
### toFixed()
toFixed() 取小數第N位,且會四捨五入,==會回傳字串==
parseFloat() 去掉小數點尾數零
```javascript
(0.666).toFixed(2); //'0.67'
let num = 123.4005;
console.log(parseFloat(num.toFixed(2)) //123.4
```
:::
### <font class="h4">➤Math.floor() 最大整數/無條件捨去</font>
```javascript
Math.floor(3.14) // 3
Math.floor(5.4) // 5
Math.floor(-5.4) // -6
Math.floor("-5.5") // -6
```
:::info
`>>0`等於`Math.floor()`,也是取整的寫法,也可以寫`|0`, 或是`~~`
```javascript
3.14>>0 //3
3.14|0 //3
~~3.14 //3
```
:::
<br>
```javascript!
3.14>>0 //3
3.14|0 //3
5.4>>0 //5
5.4|0 //5
-5.4>>0 //-5
-5.4|0 //-5
"-5.5">>0 //-5
"-5.5"|0 //-5
```
### <font class="h4">➤Math.ceil() 最小整數/無條件進位</font>
```javascript
Math.ceil(3.14) // 4
Math.ceil(5.4) // 6
Math.ceil(-5.4) // -5
Math.ceil("-5.5") // -5
```
<br>
### <font class="h4">➤Math.abs() 絕對值/取正整數</font>
任何一個數值返回一個正整數
```javascript
Math.round(3.14) // 3
Math.abs("-12") // 12
Math.abs("-0012") // 12
Math.abs(-12) // 12
Math.abs("00.032") // 0.032
Math.abs("24-5") // NaN
Math.abs("Hello") // NaN
```
<br>
### <font class="h4">➤Math.min()</font>
Math.min() 方法接受多個參數,並回傳當中最小的數
```javascript
Math.min(1, 2, 3, 4, 5) // 1
```
```javascript
Math.min() //Infinity
```
```javascript
Math.min()<Math.max() //true
```
```javascript
Math.min()>Math.max() //false
```
<br>
### <font class="h4">➤Math.max()</font>
Math.max() 方法接受多個參數,並回傳當中最大的數
```javascript
Math.max(1, 2, 3, 4, 5) // 5
```
```javascript
Math.max() //-Infinity
```
:::info
方法一.
`Math.max()`和 `Math.min()` 不接受物件陣列參數,若需要取陣列中最大或是最小數可以用下列方法
` apply` 的第一個參數可以自定義的該函數所指向的 this,在此例中不需特別指定,帶入 null 即可。第二個參數為呼叫 `apply` 方法的方法(即下例的 `Math.max` 方法)預設帶入的參數,要注意的是須改以物件形式傳入,這是 apply API 的規範。
```javascript
Math.max.apply(null, [1, 2, 3, 4, 5]) // 5
```
<br>
方法二.
也可以使用 ES6 Spread 將陣列拆解後帶入
```javascript
console.log(...[1, 2, 3, 4, 5]) // 1 2 3 4 5
```
```javascript
Math.max(...[1, 2, 3, 4, 5]) // 5
```
:::
<br>
### <font class="h4">➤Math.trunc()</font>
除去小數位來返回浮點數的整數部分
```javascript
Math.trunc(15.56) //15
Math.trunc(-15.56) //-15
Math.trunc(0.236) //0
```
### <font class="h4">➤Math.random()</font>
:arrow_right:[隨機取值](https://hackmd.io/@grayshine/Skqy-5yvY)
`Math.random()` 回傳一個介於 0 到 1 的隨機數(包含 0,不包含1)
`Math.random()` 和 `Math.floor()` 一起使用,可以回傳一個隨機整數。
```javascript
Math.floor(Math.random() * 10); //0到9的隨機整數
```
```javascript
Math.floor(Math.random() * 100); //從0到99的隨機整數
```
```javascript
Math.floor(Math.random() * 101)//從0到100的隨機整數
```
```javascript
Math.floor((Math.random() * 10) + 11);//從11到20的隨機整數
```
```javascript
Math.floor(Math.random() * 100) + 1;//從1到100的隨機整數
```
<br>
### <font class="h4">➤Math.pow()</font>
Math.pow(x, y) 方法接收兩個參數,回傳 x 的 y 次方
```javascript
Math.pow(2, 3) // 8
Math.pow(5, 2) // 25
```