题解 | #乘法#

乘法

http://www.nowcoder.com/practice/6429776e4630435cbc3eeb36bdf41f83

function multiply(a, b) {
// 先将数字转换为字符串
let strA = "" + a
let strB = "" + b
// 先获取两个数的小数位数
let lenA = (strA.indexOf(".")==-1)?0:(strA.length-strA.indexOf(".")-1)
let lenB = (strB.indexOf(".")==-1)?0:(strB.length-strB.indexOf(".")-1)
// 比较两数的精度,精度大的作为结果数精度
let len = Math.max(lenA,lenB)
// 计算运算结果
let result=null
result=parseFloat(a*b).toFixed(len)
return result
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务