TypeScript 與 JavaScript 類似,支援 Number 對象。
Number 對象是原始數值的包裝對象。
注意: 如果一個參數值不能轉換為一個數字将傳回 NaN (非數字值)。
下表列出了 Number 對象支援的屬性:
序号
屬性 & 描述
1.
<b>MAX_VALUE</b>
可表示的最大的數,MAX_VALUE 屬性值接近于 1.79E+308。大于 MAX_VALUE 的值代表 "Infinity"。
2.
<b>MIN_VALUE</b>
可表示的最小的數,即最接近 0 的正數 (實際上不會變成 0)。最大的負數是 -MIN_VALUE,MIN_VALUE 的值約為 5e-324。小于 MIN_VALUE ("underflow values") 的值将會轉換為 0。
3.
<b>NaN</b>
非數字值(Not-A-Number)。
4.
<b>NEGATIVE_INFINITY</b>
負無窮大,溢出時傳回該值。該值小于 MIN_VALUE。
5.
<b>POSITIVE_INFINITY</b>
正無窮大,溢出時傳回該值。該值大于 MAX_VALUE。
6.
<b>prototype</b>
Number 對象的靜态屬性。使您有能力向對象添加屬性和方法。
7.
<b>constructor</b>
傳回對建立此對象的 Number 函數的引用。
console.log("TypeScript Number 屬性: ");
console.log("最大值為: " + Number.MAX_VALUE);
console.log("最小值為: " + Number.MIN_VALUE);
console.log("負無窮大: " + Number.NEGATIVE_INFINITY);
console.log("正無窮大:" + Number.POSITIVE_INFINITY);
編譯以上代碼,得到以下 JavaScript 代碼:
輸出結果為:
var month = 0
if( month<=0 || month >12) {
month = Number.NaN
console.log("月份是:"+ month)
} else {
console.log("輸入月份數值正确。")
}
var month = 0;
if (month <= 0 || month > 12) {
month = Number.NaN;
console.log("月份是:" + month);
else {
console.log("輸入月份數值正确。");
function employee(id:number,name:string) {
this.id = id
this.name = name
var emp = new employee(123,"admin")
employee.prototype.email = "[email protected]"
console.log("員工号: "+emp.id)
console.log("員工姓名: "+emp.name)
console.log("員工郵箱: "+emp.email)
function employee(id, name) {
this.id = id;
this.name = name;
var emp = new employee(123, "admin");
employee.prototype.email = "[email protected]";
console.log("員工号: " + emp.id);
console.log("員工姓名: " + emp.name);
console.log("員工郵箱: " + emp.email);
Number對象 支援以下方法:
方法 & 描述
執行個體
toExponential()
把對象的值轉換為指數計數法。
toFixed()
把數字轉換為字元串,并對小數點指定位數。
toLocaleString()
把數字轉換為字元串,使用本地數字格式順序。
toPrecision()
把數字格式化為指定的長度。
toString()
把數字轉換為字元串,使用指定的基數。數字的基數是 2 ~ 36 之間的整數。若省略該參數,則使用基數 10。
valueOf()
傳回一個 Number 對象的原始數字值。