這篇文章主要介紹了ASP(VBScript)中整除和取餘方法,需要的朋友可以參考下
整除
ASP(VBScript) 中整除用/,例如m = 5 / 2,結果為2。
取餘
ASP(VBScript) 中取餘用mod,例如m = 5 mod 2,結果為1。
大數注意
m = 4444444444 / 2
n = 4444444444 / 2
第一句是正確的,第二句執行時會報溢位錯誤,因為:在整除、取餘運算前,數值運算式會四捨五入為Byte、Integer 或Long 子型別運算式。 Long 子類型的範圍是[-2147483648, 2147483647],也就是說,要進入整除或取餘的數字必須在這個範圍內。
asp中的幾個取整函數
asp中的幾個取整函數是:fix(),int(),round();
Int(number)、Fix(number)函數傳回數字的整數部分。 number 參數可以是任意有效的數值運算式。如果number 參數包含Null,則傳回Null。
例:
複製代碼代碼如下:
response.write int(2.14) '2
response.write fix(2.14) '2
response.write int(2.54) '2
response.write int(2.54) '2
Int 和Fix 函數都刪除number 參數的小數部分並傳回以整數表示的結果。 Int 和Fix 函數的差異在於如果number 參數為負數時,Int 函數會傳回小於或等於number 的第一個負整數,而Fix 函數會傳回大於或等於number 參數的第一個負整數。例如,Int 將-8.4 轉換為-9,而Fix 函數將-8.4 轉換為-8。
round(Expression[, numdecimalplaces])傳回指定位數進行四捨五入的數值。 Expression是必選項。數值表達式被四捨五入。 Numdecimalplaces是可選。數字表示小數點右邊有多少位進行四捨五入。如果省略,則Round 函數傳回整數。
例:
複製代碼代碼如下:
response.write round(3.14) '3
response.write round(3.55) '4
response.write round(3.1415,3) ' 3.142
測試程式碼:
- <%
- response.write650/100&<br>
- response.writeint(650/100)&<br>
- response.writefix(650/100)&<br>
- response.writeint(2.54)&<br>
- response.writeint(2.54)&<br>
- %>