How can I specify the base for Math.log() in JavaScript?

后端 未结 10 1430
别那么骄傲
别那么骄傲 2020-11-28 18:02

I need a log function for JavaScript, but it needs to be base 10. I can\'t see any listing for this, so I\'m assuming it\'s not possible. Are there any math wiz

10条回答
  •  粉色の甜心
    2020-11-28 18:29

    Math.log10 = function(n) {
        return (Math.log(n)) / (Math.log(10));
    }
    

    Then you can do

    Math.log10(your_number);
    

    NOTE: Initially I thought to do Math.prototype.log10 = ... to do this, but user CMS pointed out that Math doesn't work this way, so I edited out the .prototype part.

提交回复
热议问题