- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript 與 JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - Any
- TypeScript - Never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null 與 undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策制定
- TypeScript - If 語句
- TypeScript - If Else 語句
- TypeScript - 巢狀 If 語句
- TypeScript - Switch 語句
- TypeScript - 迴圈
- TypeScript - For 迴圈
- TypeScript - While 迴圈
- TypeScript - Do While 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - Rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 擴充套件介面
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 訪問器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - Keyof 型別運算子
- TypeScript - Typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 其他
- TypeScript - 三斜槓指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 實用程式型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - 數字
TypeScript 就像 JavaScript 一樣,支援數字值作為 Number 物件。數字物件將數字字面量轉換為 Number 類的例項。Number 類充當包裝器,並允許像操作物件一樣運算元字字面量。
TypeScript 的number 型別表示數值。所有數字都表示為浮點數。TypeScript 還支援 ECMAScript 2015 中引入的二進位制、八進位制和十六進位制數字。
在 TypeScript 中,我們可以建立數字原始值以及 Number 物件。
語法
我們可以使用冒號 (:) 在變數名之後宣告 number 型別的變數,然後是 number −
let varName: number = value;
在上面的語法中,我們聲明瞭一個名為 varName 的 number 型別的變數。此處 value 是任何數值,例如十進位制、二進位制、八進位制或十六進位制數字。
我們可以建立 Number 物件。要建立 Number 物件,我們可以使用 Number() 建構函式,如下所示 −
var var_name = new Number(value)
如果將非數字引數作為引數傳遞給 Number 的建構函式,則它將返回 NaN(非數字)。
型別 'Number' 是一個包裝物件,但型別 'number' 是一個原始型別。儘可能使用 'number'。型別 'Number' 無法分配給型別 'number'。
建立數字型別
在下面的示例中,我們建立了一個 number 型別的變數 count。我們將 10 分配給 count。
let count: number = 10; console.log(count);
編譯後,它將生成以下 JavaScript 程式碼。
let count = 10; console.log(count);
輸出如下 −
10
我們還可以將浮點數、二進位制、八進位制和十六進位制值分配給 number 型別的變數。檢視下面的 TypeScript 程式碼片段 –
let decNum: number = 10.6; // floating point number let binNum: number = 0b101001; // binary number let octNum: number = 0o45; // octal number let hexNum: number = 0x80fd; // hexadecimal number
建立 Number 物件
const count = new Number(10); console.log(count); console.log(typeof count);
編譯後,它將生成相同的 JavaScript 程式碼。
上述示例程式碼的輸出如下 –
[Number: 10] Object
Number 屬性
下表列出了 Number 物件的一組屬性 −
| 序號 | 屬性 & 描述 |
|---|---|
| 1. | MAX_VALUE JavaScript 中數字可以具有的最大可能值 1.7976931348623157E+308。 |
| 2. | MIN_VALUE JavaScript 中數字可以具有的最小可能值 5E-324。 |
| 3. | NaN 等於非數字的值。 |
| 4. | NEGATIVE_INFINITY 小於 MIN_VALUE 的值。 |
| 5. | POSITIVE_INFINITY 大於 MAX_VALUE 的值。 |
| 6. | prototype Number 物件的靜態屬性。使用 prototype 屬性將新的屬性和方法分配給當前文件中的 Number 物件。 |
| 7. | constructor 返回建立此物件例項的函式。預設情況下,它是 Number 物件。 |
示例
console.log("TypeScript Number Properties: ");
console.log("Maximum value that a number variable can hold: " + Number.MAX_VALUE);
console.log("The least value that a number variable can hold: " + Number.MIN_VALUE);
console.log("Value of Negative Infinity: " + Number.NEGATIVE_INFINITY);
console.log("Value of Negative Infinity:" + Number.POSITIVE_INFINITY);
編譯後,它將生成 JavaScript 中的相同程式碼。
其輸出如下 −
TypeScript Number Properties: Maximum value that a number variable can hold: 1.7976931348623157e+308 The least value that a number variable can hold: 5e-324 Value of Negative Infinity: -Infinity Value of Negative Infinity:Infinity
示例:NaN
var month = 0
if( month<=0 || month >12) {
month = Number.NaN
console.log("Month is "+ month)
} else {
console.log("Value Accepted..")
}
編譯後,它將生成 JavaScript 中的相同程式碼。
其輸出如下 −
Month is NaN
示例:prototype
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"Smith")
employee.prototype.email = "smith@abc.com"
console.log("Employee's Id: "+emp.id)
console.log("Employee's name: "+emp.name)
console.log("Employee's Email ID: "+emp.email)
編譯後,它將生成以下 JavaScript 程式碼 −
//Generated by typescript 1.8.10
function employee(id, name) {
this.id = id;
this.name = name;
}
var emp = new employee(123, "Smith");
employee.prototype.email = "smith@abc.com";
console.log("Employee 's Id: " + emp.id);
console.log("Employee's name: " + emp.name);
console.log("Employee's Email ID: " + emp.email);
其輸出如下 −
Employee's Id: 123 Emaployee's name: Smith Employee's Email ID: smith@abc.com
Number 方法
Number 物件僅包含預設方法,這些方法是每個物件定義的一部分。一些常用的方法列在下面 −
| 序號 | 方法 & 描述 |
|---|---|
| 1. | toExponential()
強制數字以指數表示法顯示,即使該數字處於 JavaScript 通常使用標準表示法的範圍內。 |
| 2. | toFixed()
格式化數字,使其在小數點右側具有特定數量的數字。 |
| 3. | toLocaleString()
返回當前數字的字串值版本,其格式可能因瀏覽器的本地設定而異。 |
| 4. | toPrecision()
定義要顯示數字的總位數(包括小數點左側和小數點右側的數字)。負精度將引發錯誤。 |
| 5. | toString()
返回數字值的字串表示形式。該函式傳遞基數,一個介於 2 和 36 之間的整數,指定用於表示數值的基數。 |
| 6. | valueOf()
返回數字的原始值。 |