加法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function
numAdd(num1, num2) {
var
baseNum, baseNum1, baseNum2;
try
{
baseNum1 = num1.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum1 = 0;
}
try
{
baseNum2 = num2.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
return
(num1 * baseNum + num2 * baseNum) / baseNum;
};
|
减法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function
numSub(num1, num2) {
var
baseNum, baseNum1, baseNum2;
var
precision;
// 精度
try
{
baseNum1 = num1.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum1 = 0;
}
try
{
baseNum2 = num2.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
return
((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
};
|
乘法:
1
2
3
4
5
6
7
8
9
10
|
function
numMulti(num1, num2) {
var
baseNum = 0;
try
{
baseNum += num1.toString().split(
"."
)[1].length;
}
catch
(e) {}
try
{
baseNum += num2.toString().split(
"."
)[1].length;
}
catch
(e) {}
return
Number(num1.toString().replace(
"."
,
""
)) * Number(num2.toString().replace(
"."
,
""
)) / Math.pow(10, baseNum);
};
|
除法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function
numDiv(num1, num2) {
var
baseNum1 = 0, baseNum2 = 0;
var
baseNum3, baseNum4;
try
{
baseNum1 = num1.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum1 = 0;
}
try
{
baseNum2 = num2.toString().split(
"."
)[1].length;
}
catch
(e) {
baseNum2 = 0;
}
with
(Math) {
baseNum3 = Number(num1.toString().replace(
"."
,
""
));
baseNum4 = Number(num2.toString().replace(
"."
,
""
));
return
(baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1);
}
};
|
本文转自 爱笑嘚蛋蛋 51CTO博客,原文链接:http://blog.51cto.com/dd118/1889702,如需转载请自行联系原作者