美文网首页JavaScript
JavaScript:计算百分比

JavaScript:计算百分比

作者: 东方晓 | 来源:发表于2022-11-11 15:48 被阅读0次

2022-11-11 周五

使用到的知识点
Math.round()

Math.round() 函数返回一个数字四舍五入后最接近的整数。

/**
 * 计算百分比
 * @param   {number} num   分子
 * @param   {number} total 分母
 * @returns {number} 返回数百分比
 */
function Percentage(num, total) { 
    if (num == 0 || total == 0){
        return 0
    }
    return (Math.round(num / total * 10000) / 100.00) // 小数点后两位百分比
}

相关文章

网友评论

    本文标题:JavaScript:计算百分比

    本文链接:https://www.haomeiwen.com/subject/nioztdtx.html