<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Math对象</title>
</head>
<body>
</body>
<script>
console.log("1.random方法:", Math.random()) //返回一个0~1之间的随机浮点数,包括0不包括1
console.log("2.min方法:", Math.min(12, 33, 66)) //接受多个参数,返回最小值
console.log("3.max方法:", Math.max(12, 33, 66)) //接受多个参数,返回最小值
console.log("4.round方法-四舍五入:", Math.round(3.5)) //四舍五入
console.log("5.floor方法-向下取整:", Math.floor(3.5)) //向下取整
console.log("6.ceil方法-向上取整:", Math.ceil(3.5)) //向上取整
// import math from './4.1math.js'
console.log("7.加:", add(3, 4))
console.log("8.减:",subtract(3, 4))
console.log("9.乘:",multiply(5, 4))
console.log("10.除:",divide(36, 6))
function add(a, b) {
let c, d, e;
try {
c = a.toString().split('.')[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split('.')[1].length;
} catch (f) {
d = 0;
}
e = Math.pow(10, Math.max(c, d));
return (multiply(a, e) + multiply(b, e)) / e;
}
function subtract(a, b) {
let c, d, e;
try {
c = a.toString().split('.')[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split('.')[1].length;
} catch (f) {
d = 0;
}
e = Math.pow(10, Math.max(c, d));
return (multiply(a, e) - multiply(b, e)) / e;
}
function multiply(a, b) {
let c = 0,
d = a.toString(),
e = b.toString();
try {
c += d.split('.')[1].length;
} catch (f) {} // eslint-disable-line no-empty
try {
c += e.split('.')[1].length;
} catch (f) {} // eslint-disable-line no-empty
return (
Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
);
}
function divide(a, b) {
let c,
d,
e = 0,
f = 0;
try {
e = a.toString().split('.')[1].length;
} catch (g) {} // eslint-disable-line no-empty
try {
f = b.toString().split('.')[1].length;
} catch (g) {} // eslint-disable-line no-empty
c = Number(a.toString().replace('.', ''));
d = Number(b.toString().replace('.', ''));
return (c / d) * Math.pow(10, f - e);
}
console.log("11. 格式化小数:", numberToFixed(3.000255, 4))
console.log("12. numFix:", numFix(3.100001111, 3))
// 格式化小数,会补齐小数位,比如1.11格式化为4位就是1.1100
function numberToFixed(num, n){
if(!num) return 0;
let r = num - 0;
r = formatScienceNum(r);
let t =numFix(r, n);
let l = 0;
if(t.indexOf('.') != -1) {
let x = t.split('.')[1];
if(x) {
l = n - x.length;
}
} else {
l = n;
if(n > 0) t += '.';
}
if(l > 0) {
for(var i = 0; i < l; i ++) {
t += '0';
}
}
return (isNaN(t) || t == 'NaN') ? '' : t;
}
//将科学记数法的数字正常显示
function formatScienceNum(num) {
let n = Math.abs(num);
let r = n + '';
if((r.toLowerCase().indexOf('e') >= 0) && (n < 1)) {
r = '0' + (Number(r) + 1 + '').substring(1);
}
if(num < 0) r = '-' + r;
return r;
}
// 截取小数
function numFix(n, r) {
let re = n + '';
re = re.replace(/[^\d\.]/g, '');
if(re) {
if(re.indexOf('.') != (-1)) {
let tempRe = re.split('.');
let z = tempRe[0];
let x = '';
if(tempRe.length > 1) {
x = tempRe[1].substring(0, r);
}
if(r > 0) {
tempRe = z + '.' + x;
} else {
tempRe = z;
}
re = tempRe;
}
}
return re;
}
</script>
</html>
Math对象
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...