js取整數(shù)、取余數(shù)的方法
1.丟棄小數(shù)部分,保留整數(shù)部分
parseInt(5/2)
2.向上取整,有小數(shù)就整數(shù)部分加1
Math.ceil(5/2)
3,四舍五入.
Math.round(5/2)
4,向下取整
Math.floor(5/2)
Math 對象的方法
FF: Firefox, N: Netscape, IE: Internet Explorer
方法 描述 FF N IE
abs(x) 返回數(shù)的絕對值 1 2 3
acos(x) 返回數(shù)的反余弦值 1 2 3
asin(x) 返回數(shù)的反正弦值 1 2 3
atan(x) 以介于 -PI/2 與 PI/2 弧度之間的數(shù)值來返回 x 的反正切值 1 2 3
atan2(y,x) 返回從 x 軸到點 (x,y) 的角度(介于 -PI/2 與 PI/2 弧度之間) 1 2 3
ceil(x) 對一個數(shù)進行上舍入。 1 2 3
cos(x) 返回數(shù)的余弦 1 2 3
exp(x) 返回 e 的指數(shù)。 1 2 3
floor(x) 對一個數(shù)進行下舍入。 1 2 3
log(x) 返回數(shù)的自然對數(shù)(底為e) 1 2 3
max(x,y) 返回 x 和 y 中的最高值 1 2 3
min(x,y) 返回 x 和 y 中的最低值 1 2 3
pow(x,y) 返回 x 的 y 次冪 1 2 3
random() 返回 0 ~ 1 之間的隨機數(shù) 1 2 3
round(x) 把一個數(shù)四舍五入為最接近的整數(shù) 1 2 3
sin(x) 返回數(shù)的正弦 1 2 3
sqrt(x) 返回數(shù)的平方根 1 2 3
tan(x) 返回一個角的正切 1 2 3
toSource() 代表對象的源代碼 1 4 -
valueOf() 返回一個 Math 對象的原始值
代碼案例:
<script type="text/javascript">
//取整
function getResult(num){
return parseInt(num);
}
//四舍五入到num后面的n位
function getResult(num,n){
return Math.round(num*Math.pow(10,n))/Math.pow(10,n);
}
//截取n位
function getresult(num,n){
return num.toString().replace(new RegExp("^(\\-?\\d*\\.?\\d{0,"+n+"})(\\d*)$"),"$1")+0;
}
</script>
其他:
var mLength = textMn.length;
var mFirst = parseInt(mLength/60);
//取整
//alert(mLength);
var mLast = mLength; //取余
if(mLast>0){
$(".mood_content").height((mFirst+1)*20);
}
相關文章
javascript 實現(xiàn)動態(tài)側(cè)邊欄實例詳解
這篇文章主要介紹了javascript 實現(xiàn)動態(tài)側(cè)邊欄實例詳解的相關資料,并附實例代碼,幫助大家學習理解,需要的朋友可以參考下2016-11-11
基于BootStrap與jQuery.validate實現(xiàn)表單提交校驗功能
學習前臺后臺最開始接觸的業(yè)務都是用戶注冊和登錄,下面通過本文給大家介紹BootStrap與jQuery.validate實現(xiàn)表單提交校驗功能,感興趣的朋友一起學習吧2016-12-12
利用JavaScript編寫Python內(nèi)置函數(shù)查詢工具
Python有豐富的內(nèi)置函數(shù)實現(xiàn)各種功能,但查詢內(nèi)置函數(shù)時總是需要百度查,有沒有一個小工具可以單機無網(wǎng)絡查詢Python內(nèi)置函數(shù),方便自己學習編寫Python程序呢?本文就來用JavaScript編寫一個2023-02-02

