1.时间戳转化为时间
/***
* 时间戳转化为时间 格式为 2014-06-06 17:50:50
* @param unix
* @returns {string}
*/
function unix_to_datetime(unix) {
var now = new Date(parseInt(unix) * 1000);
var formatTime = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
return formatTime.replace(/\b(\w)\b/g,'0$1');
}
2.强制保留2位小数不四舍五入
/****
* 强制保留2位小数不四舍五入 1 => 1.00 2.69888 => 2.69
* @param x
* @returns {*}
*/
function toDecimal2(x)
{
if(x.indexOf('.') != -1)
{
var db = x;
var num = 2;
var re = new RegExp("\\d+\\.\\d{" + num + "}","gm");
return db.match(re);
}
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}