简体   繁体   中英

Javascript: Need workaround for automatic rounding

I have a javascript calculation to do:

var temp = ((1 + 0.07)^(1/12))-1;

The actual answer for this is:

0.00565414539

On the ^(1/12) (0.833..) you get 1.00565414539

However, Javascript automatically rounds this to 1.

Any fixes for this?

There probably are a million questions like this, but I couldn't find any on a quick google search and I've been mucking about with this for a while.

^ is the bitwise XOR operator, not a power of operator.

You need to do:

var temp = (Math.pow((1 + 0.07),(1/12)))-1;

尝试使用Math.pow()函数而不是^进行功效计算。

alert(Math.pow(1 + 0.07,1/12)-1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM