在javascript中求10的幂,还有比这更好的方法吗
我需要创建一个特定幂的整数值(这不是正确的术语,但基本上我需要创建 10、100、1000 等)“幂”将被指定为函数参数。我想出了一个解决方案,但它感觉很老套而且错误。我想学习一种更好的方法,如果有一种方法,也许不是基于字符串的方法?此外,eval() 也不是一个选项。
这是我此时所拥有的:
function makeMultiplierBase(precision)
{
var numToParse = '1';
for(var i = 0; i < precision; i++)
{
numToParse += '0';
}
return parseFloat(numToParse);
}
我也刚刚提出了这个基于非字符串的解决方案,但由于循环而仍然显得很老套:
function a(precision)
{
var tmp = 10;
for(var i = 1; i < precision; i++)
{
tmp *= 10;
}
return tmp;
}
顺便说一句,我需要这样做来创建一个用于处理货币的舍入方法。我一直在使用 var formatted = Math.round(value * 100) / 100
但是这段代码到处都显示出来,我希望有一个方法来处理舍入到特定精度,所以我创建了这个
if(!Math.roundToPrecision)
{
Math.roundToPrecision = function(value, precision)
{
Guard.NotNull(value, 'value');
b = Math.pow(10, precision);
return Math.round(value * b) / b;
}
}
想法我会在这里包含它因为事实证明它已经很方便了。
I have a need to create an integer value to a specific power (that's not the correct term, but basically I need to create 10, 100, 1000, etc.) The "power" will be specified as a function parameter. I came up with a solution but MAN does it feel hacky and wrong. I'd like to learn a better way if there is one, maybe one that isn't string based? Also, eval() is not an option.
Here is what I have at this time:
function makeMultiplierBase(precision)
{
var numToParse = '1';
for(var i = 0; i < precision; i++)
{
numToParse += '0';
}
return parseFloat(numToParse);
}
I also just came up with this non-string based solution, but still seems hacky due to the loop:
function a(precision)
{
var tmp = 10;
for(var i = 1; i < precision; i++)
{
tmp *= 10;
}
return tmp;
}
BTW, I needed to do this to create a rounding method for working with currency. I had been using
var formatted = Math.round(value * 100) / 100
but this code was showing up all over the place and I wanted to have a method take care of the rounding to a specific precision so I created this
if(!Math.roundToPrecision)
{
Math.roundToPrecision = function(value, precision)
{
Guard.NotNull(value, 'value');
b = Math.pow(10, precision);
return Math.round(value * b) / b;
}
}
Thought I'd include this here as it's proven to be handy already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 ES5 及更早版本中,使用 Math.pow:
在 ES2016 及更高版本中,使用 求幂运算符:
In ES5 and earlier, use
Math.pow
:In ES2016 and later, use the exponentiation operator:
为什么不:
Why not:
如果您需要做的只是将 10 提高到不同的幂,或者任何底数的任何幂,为什么不使用内置的
Math.pow(10,power);
除非您有特定的需要重新发明轮子if all you need to do is raise 10 to different powers, or any base to any power why not use the built in
Math.pow(10,power);
unless you have soe specific need to reason to reinvent the wheel这与您的功能具有相同的结果,但我仍然不明白其应用/意图。
This has the same result as your function, but i still don't understand the application/intention.
对于 10 立方及以上的幂,
Math.pow()
可能会失去精度。例如:虽然在 JavaScript 中这不是一个日常问题,但在某些情况下可能会很麻烦,尤其是使用比较运算符时。一个例子是 Google 的
Closure 库中的 log10Floor()
函数:如果您传递 10 立方以上的 10 次方,此函数可能会返回错误结果,因为
Math.pow(10, 33)> 1e33
计算结果为true
。我解决这个问题的方法是使用数字强制,将指数连接到“1e”:并且修复
log10Floor()
函数:注意:闭包库中的错误此后已修复。
For powers at 10³³ and above,
Math.pow()
may lose precision. For example:While not an everyday problem that you may run into in JavaScript, it could be quite troublesome in some situations, particularly with comparison operators. One example is Google's
log10Floor()
function from the Closure Library:If you pass a power of 10 above 10³³, this function could return an incorrect result because
Math.pow(10, 33) > 1e33
evaluates totrue
. The way I worked around this is to use Number coercion, concatenating the exponent to '1e':And, fixing the
log10Floor()
function:Note: The bug in closure library has since been fixed.
使用查找表。但如果这是为了四舍五入货币金额,您应该使用 BigDecimal 而不是整个 schemozzle。
Use a lookup table. But if this is for rounding currency amounts, you should be using BigDecimal instead of the entire schemozzle.
我只是在浏览 https://github.com/aecostas/huffman 时偶然发现了一些东西。编译后的代码(js)有一行
如果您尝试评估 9e9(在节点和浏览器控制台上),它会给您 9000000000,即“9*10^9”。基于此,您可以简单地执行以下操作来获得第 10 个力量。
<代码>
var n = 2;
评估(“1e”+n); //输出100
编辑
:有关指数表示法的更多信息
http://www.2ality.com/2012/03/displaying-numbers.html 。
I just stumbled on something while going through https://github.com/aecostas/huffman. The compiled code(js) has a line
If you try to evaluate 9e9 (on the node and browser console) it gives you 9000000000 which is "9*10^9".Based on that you could simply do the below to get the 10th power.
var n = 2;
eval("1e"+n); //outputs 100
EDIT: More on exponential notation from
http://www.2ality.com/2012/03/displaying-numbers.html.