js常用計算方法整理

2021-08-28 14:36:52 字數 1248 閱讀 1851

一、除法取整

math.round(x) // 四捨五入,如math.round(0.60),結果為1;math.round(0.49),結果為0;

math.floor(x) // 向下捨入,如math.floor(0.60)與math.floor(0.49),結果均為0;

math.ceil(x) //向上捨入,如math.ceil(0.60)與math.ceil(0. 49),結果均為1。

二、日期計算

1、常用方法

var now = new date();

var yy = now.getfullyear(); //年

var mm = now.getmonth() + 1; //月

var dd = now.getdate(); //日

var hh = now.gethours(); //時

var ii = now.getminutes(); //分

var ss = now.getseconds(); //秒

var timestamp = parseint(now .gettime()/1000); // 當前時間戳

2、日期轉字串

datetostring: function(date) 

if (day.length == 1)

var datetime = year + "-" + month + "-" + day;

return datetime;

},

3、字串轉日期

stringtodate : function(datestr,separator)

var datearr = datestr.split(separator);

var year = parseint(datearr[0]);

var month;

//處理月份為04這樣的情況

if(datearr[1].indexof("0") == 0)else

var day = parseint(datearr[2]);

var date = new date(year,month -1,day);

return date;

},

js 常用計算

由於存在計算精度的問題,例如0.1 0.2 0.30000000000000004,所以需要整理以下方法,方便進行簡單計算。主要思路是先轉成整數,然後再進行計算,計算完再轉回浮點數 獲取小數字以及向右移動小數字,是計算時轉換成整數的工具函式。加減乘除四個函式都用到了 獲取小數字 export fun...

c語言程式設計中常用計算方法

上完課過來整理一下筆記555 1 窮舉法1 百錢買百雞問題 簡化後 23 include 4 include 5 6int main 718 19 20 return0 21 2 迭代法 1 求解一元三次方程指定範圍的根 2 include 3 include 4 include 5 6double...

JS方法常用方法整理

字串的常用屬性 date物件的用法 var dt new date var dt new date 一種特殊的寫法,只適用於這個date的物件 dt.getfullyear 年 dt.getmonth 月 從0開始 dt.getdate 日 dt.gethours 小時 dt.getminutes ...