TypeScript 算術(shù)運(yùn)算符示例

2018-12-26 16:26 更新

TypeScript 算術(shù)運(yùn)算符示例

假設(shè)變量a和b中的值分別為10和5。

運(yùn)算符描述示例
+(加法)返回操作數(shù)的總和a + b = 15
-(減法)返回值的差a - b = 5
*(乘法)返回值的乘積a * b = 50
 /(除法)執(zhí)行除法運(yùn)算并返回商a / b = 2
%(取余)執(zhí)行除法并返回余數(shù)a%b = 0
++(遞增)將變量的值增加1a ++ = 11
--(遞減)將變量的值減少1a-- = 9

示例

var num1:number = 10 
var num2:number = 2
var res:number = 0
 
res = num1 + num2 
console.log("Sum:        "+res); 

res = num1 - num2; 
console.log("Difference: "+res) 

res = num1*num2 
console.log("Product:    "+res) 

res = num1/num2 
console.log("Quotient:   "+res)
 
res = num1%num2 
console.log("Remainder:   "+res) 

num1++ 
console.log("Value of num1 after increment "+num1) 

num2-- 
console.log("Value of num2 after decrement "+num2)

在編譯時(shí),它將生成以下的JavaScript代碼:

//Generated by typescript 1.8.10
var num1 = 10;
var num2 = 2;
var res = 0;

res = num1 + num2;
console.log("Sum:        " + res);

res = num1 - num2;
console.log("Difference: " + res);

res = num1 * num2;
console.log("Product:    " + res);

res = num1 / num2;
console.log("Quotient:   " + res);

res = num1 % num2;
console.log("Remainder:   " + res);

num1++;
console.log("Value of num1 after increment " + num1);

num2--;
console.log("Value of num2 after decrement " + num2);

上述程序的輸出如下:

Sum: 12 
Difference: 8 
Product: 20 
Quotient : 5 
Remainder: 0 
Value of num1 after increment: 11 
Value of num2 after decrement: 1




以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)