function product (num1
, num2
) {
var sum
= []
var fuhao
= ''
var point
= 0
if (num1
== 0 || num2
== 0) return '0'
if(num1
[0] == '-') fuhao
= '-'
if(num2
[0] == '-' ){
fuhao
= fuhao
? '' : '-'
}
if(num1
.indexOf('.') != -1){
point
+= num1
.length
- num1
.indexOf('.') - 1
}
if(num2
.indexOf('.') != -1){
point
+= num2
.length
- num2
.indexOf('.') - 1
}
num1
=num1
.replace("-","")
num2
=num2
.replace("-","")
num1
=num1
.replace(".","")
num2
=num2
.replace(".","")
var success
= true
for(var i
= 0; i
< num1
.length
;i
++){
for(var j
=0;j
<num2
.length
;j
++){
let product
= num1
[num1
.length
- 1 - i
] * num2
[num2
.length
- 1 - j
]
if(!product
&& product
!= 0){
success
= false
}
let un1
= product
% 10
let un2
= Math
.floor(product
/10)
if(!sum
[i
+j
]) sum
[i
+j
] = 0
if(!sum
[i
+j
+1]) sum
[i
+j
+1] = 0
if(!sum
[i
+j
+2]) sum
[i
+j
+2] = 0
let sum1
= sum
[i
+j
] - 0 + un1
sum
[i
+j
] = sum1
% 10
let sum2
= sum
[i
+j
+1] - 0 + un2
+ Math
.floor(sum1
/10)
sum
[i
+j
+1] = sum2
% 10
sum
[i
+j
+ 2] = sum
[i
+j
+ 2] - 0 + Math
.floor(sum2
/10)
}
}
if(!success
){
return false
}
var str
= sum
.reverse().join('')
if (point
!= 0){
var str
= str
.slice(0, str
.length
- point
) + '.' + str
.slice(-point
)
while(true){
var i
= str
.length
- 1
if(str
[i
] == 0){
str
= str
.slice(0, i
)
}else if(str
[i
] == '.'){
str
= str
.slice(0, i
)
break
}else {
break
}
}
while(true){
if(str
[0] == 0 && str
[1] && str
[1]!='.'){
str
= str
.slice(1, str
.length
)
}else {
break
}
}
} else{
while(true){
if(str
[0] == 0){
str
= str
.slice(1, str
.length
)
}else {
break
}
}
}
if(fuhao
){
str
= '-' + str
}
return str
}
转载请注明原文地址:https://tech.qufami.com/read-16405.html