js计算百分比

function GetPercent(num, total) {
    /// <summary>
    /// 求百分比
    /// </summary>
    /// <param name="num">当前数</param>
    /// <param name="total">总数</param>
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00)+"%";
}

使用方法如下:

GetPercent(1,100)
结果:
1%;
posted @ 2016-05-19 10:38  小小高  阅读(49740)  评论(0编辑  收藏  举报