Commit 5b389c06 authored by 李硕's avatar 李硕

添加方法

parent 140b1327
...@@ -120,6 +120,23 @@ const formatTime = function dateFormat(thisDate, fmt = "yyyy-MM-dd hh:mm:ss") { ...@@ -120,6 +120,23 @@ const formatTime = function dateFormat(thisDate, fmt = "yyyy-MM-dd hh:mm:ss") {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt; return fmt;
} }
//时间戳转为>2020/09/14
const formatDate = function (secs) { //123456789 -->
var t = new Date(secs);
var year = t.getFullYear();
var month = t.getMonth() + 1;
if (month < 10) { month = '0' + month };
var date = t.getDate();
if (date < 10) { date = '0' + date };
// var hour = t.getHours()
// if (hour < 10) { hour = '0' + hour }
// var minute = t.getMinutes()
// if (minute < 10) { minute = '0' + minute }
// var second = t.getSeconds()
// if (second < 10) { second = '0' + second }
return year + '/' + month + '/' + date;
// + hour + ':' + minute + ':' + second
};
function MathRand() { function MathRand() {
var Num = ""; var Num = "";
...@@ -128,6 +145,46 @@ function MathRand() { ...@@ -128,6 +145,46 @@ function MathRand() {
} }
return Num; return Num;
} }
// 解决小数相加精度问题
function addFloat(num1, num2) {
var r1, r2, m;
r1 = num1.toString().split(".")[1] ? num1.toString().split(".")[1].length : 0;
r2 = num2.toString().split(".")[1] ? num2.toString().split(".")[1].length : 0;
m = Math.pow(10, Math.max(r1, r2));
return (num1 * m + num2 * m) / m;
}
// 小数相除
const accMul = function (arg1, arg2) {
var m = 0,
s1 = arg1.toString();
s2 = arg2.toString();
try {
m += s1.split(".")[1].length;
} catch (e) { }
try {
m += s2.split(".")[1].length;
} catch (e) { }
return (
(Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) /
Math.pow(10, m)
);
};
// 小数相乘
const signFigures = function (num, rank = 6) {
if (!num) return 0;
const sign = num / Math.abs(num);
const number = num * sign;
const temp = rank - 1 - Math.floor(Math.log10(number));
let ans;
if (temp > 0) {
ans = parseFloat(number.toFixed(temp));
} else if (temp < 0) {
ans = Math.round(number / Math.pow(10, temp)) * temp;
} else {
ans = Math.round(number);
}
return ans * sign;
};
module.exports = { module.exports = {
loginfo, loginfo,
...@@ -135,5 +192,9 @@ module.exports = { ...@@ -135,5 +192,9 @@ module.exports = {
passUrlList, passUrlList,
urlCheck, urlCheck,
isNewDay, isNewDay,
formatTime formatTime,
addFloat,
formatDate,
accMul,
signFigures
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment