[removed] calculate x% of a number

后端 未结 10 1816
-上瘾入骨i
-上瘾入骨i 2020-12-12 15:07

I am wondering how in javascript if i was given a number (say 10000) and then was given a percentage (say 35.8%)

how would I work out how much that is (eg 3580)

10条回答
  •  一向
    一向 (楼主)
    2020-12-12 15:31

    It may be a bit pedantic / redundant with its numeric casting, but here's a safe function to calculate percentage of a given number:

    function getPerc(num, percent) {
        return Number(num) - ((Number(percent) / 100) * Number(num));
    }
    
    // Usage: getPerc(10000, 25);
    

提交回复
热议问题