excel的IRR函数
office官网找到IRR的介绍
https://support.office.com/zh-cn/article/irr-%E5%87%BD%E6%95%B0-64925eaa-9988-495b-b290-3ad0c163c1bc
https://docs.microsoft.com/zh-cn/office/vba/language/reference/user-interface-help/irr-function?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev11.query%3FappId%3DDev11IDEF1%26l%3Dzh-CN%26k%3Dk(vblr6.chm1009282)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv15)%26rd%3Dtrue
1.找js的实现
function IRR(cashFlows, estimatedResult) {
var result = "isNAN";
if (cashFlows != null && cashFlows.length > 0) {
// check if business startup costs is not zero:
if (cashFlows[0] != 0) {
var noOfCashFlows = cashFlows.length;
var sumCashFlows = 0;
// check if at least 1 positive and 1 negative cash flow exists:
var noOfNegativeCashFlows = 0;
var noOfPositiveCashFlows = 0;
for (var i = 0; i < noOfCashFlows; i++) {
sumCashFlows += cashFlows[i];
if (cashFlows[i] > 0) {
noOfPositiveCashFlows++;
} else {
if (cashFlows[i] < 0) {
noOfNegativeCashFlows++;
}
}
}
// at least 1 negative and 1 positive cash flow available?
if (noOfNegativeCashFlows > 0 && noOfPositiveCashFlows > 0) {
// set estimated result:
var irrGuess = 0.1; // default: 10%
if (!isNaN(estimatedResult)) {
irrGuess = estimatedResult;
if (irrGuess <= 0) {
irrGuess = 0.5;
}
}
// initialize first IRR with estimated result:
var irr = 0;
if (sumCashFlows < 0) { // sum of cash flows negative?
irr = -irrGuess;
} else { // sum of cash flows not negative
irr = irrGuess;
}
// iteration:
// the smaller the distance, the smaller the interpolation
// error
var minDistance = 1e-15;
// business startup costs
var cashFlowStart = cashFlows[0];
var maxIteration = 100;
var wasHi = false;
var cashValue = 0;
for (var i = 0; i <= maxIteration; i++) {
// calculate cash value with current irr:
cashValue = cashFlowStart; // init with startup costs
// for each cash flow
for (var j = 1; j < noOfCashFlows; j++) {
cashValue += cashFlows[j] / Math.pow(1 + irr, j);
}
// cash value is nearly zero
if (Math.abs(cashValue) < 0.01) {
result = irr;
break;
}
// adjust irr for next iteration:
// cash value > 0 => next irr > current irr
if (cashValue > 0) {
if (wasHi) {
irrGuess /= 2;
}
irr += irrGuess;
if (wasHi) {
irrGuess -= minDistance;
wasHi = false;
}
} else {// cash value < 0 => next irr < current irr
irrGuess /= 2;
irr -= irrGuess;
wasHi = true;
}
// estimated result too small to continue => end
// calculation
if (irrGuess <= minDistance) {
result = irr;
break;
}
}
}
}
}
return result;
}
2. var一个 [] 来验证找到的函数 是否可用
3.调用IRR,estimatedResult设置为0.1,保持与excel默认值一致
excel的IRR函数的更多相关文章
- 浅谈Excel开发:六 Excel 异步自定义函数
上文介绍了Excel中的自定义函数(UDF ),它极大地扩展了Excel插件的功能,使得我们可以将业务逻辑以Excel函数的形式表示,并可以根据这些细粒度的自定义函数,构建各种复杂的分析报表. 普通的 ...
- 【Excel 4.0 函数】REGISTER
REGISTER.ID 返回指定的 DLL 或 代码资源注册过的函数 ID.如果 DLL 或 代码资源没有注册,这个函数将会注册它们,并返回 注册ID. REGISTER.ID 可以用于工作表(不同于 ...
- Excel中COUNTIFS函数统计词频个数出现次数
Excel中COUNTIFS函数统计词频个数出现次数 在Excel中经常需要实现如下需求:在某一列单元格中有不同的词语,有些词语相同,有的不同(如图1所示).需要统计Excel表格中每个词语出现的 ...
- [VBA]用一个简单例子说明如何在Excel中自定义函数
Excel中的函数无疑是强大的,但是再强大的战士也有他脆弱的脚后跟[1].这两天在使用Excel的时候遇到了一个需求,要在某一个单元格里面自动计算今天是星期几(如显示 Today is Tuesday ...
- Excel中choose函数的使用方法
你还在为Excel中choose函数的使用方法而苦恼吗,今天小编教你Excel中choose函数的使用方法,让你告别Excel中choose函数的使用方法的烦恼. 经验主要从四方面对Excel函数进行 ...
- Excel公式与函数——每天学一个
说明(2018-5-29 20:35:53): 1. 根据刘伟的视频讲解进行总结,网上讲Excel公式与函数的貌似就他讲的还不错.在他的微博里看到现在的照片胖了不少,不过还挺帅的,不再是以前那个小屌丝 ...
- Excel中Sumproduct函数的使用方法
1.sumproduct函数的含义 1 1.Sumproduct函数的适用范围,在给定的几组数组中,然后把数组间对应的元素相乘,最后返回乘积之和. 从字面上可以看出,sumproduct有两个英文单词 ...
- Excel中concatenate函数的使用方法
你还在为Excel中concatenate函数的使用方法而苦恼吗,今天小编教你Excel中concatenate函数的使用方法,让你告别Excel中concatenate函数的使用方法的烦恼. 经验主 ...
- Excel中IF函数的嵌套用法(多条件)
Excel中IF函数的嵌套用法(多条件) Excel中IF函数的嵌套用法(多条件) 函数格式:if(logical_test,value_if_true,value_if_false).其中: ...
随机推荐
- django实战(三)--删除和批量删除
上一节我们完成了带分页的模糊查询,本节我们继续删除操作. 同样的,先给出各个模块的代码(上一节的就不在贴出了):urls.py path('curd/delete/',views.curd_delet ...
- OpenCV vs. Armadillo vs. Eigen on Linux
OpenCV vs. Armadillo vs. Eigen on Linux From:http://nghiaho.com/?p=936 In this post I’ll be comparin ...
- VUE方法
1.$event 变量 $event 变量用于访问原生DOM事件. <!DOCTYPE html> <html lang="zh"> <head> ...
- 彩虹表(rainbow table)
前记 MD5的全称是Message-Digest Algorithm 5(信息-摘要算法): 特点是不可逆的,一般解密不了:那有没有想过,为什么各种工具网站都可以进行MD5解密呢?https://ww ...
- 在 ubuntu 下如何压缩与解压 7zip 文件。
1. 首先利用 ubuntu 软件中心搜索并下载 7zip.或者在终端中输入:sudo apt-get install p7zip 2. 压缩 3. 解压 谢谢浏览!
- html页面的渲染And<script>位置的影响
周末加班敲代码的时用到了<script>标签,突然想到了一个问题:别的自测项目里面<script>我把他放在了不同位置,这里应该会对代码的执行与渲染后影响吧?于是今天专门进行了 ...
- 我是如何一步步编码完成万仓网ERP系统的(十二)库存 1.概述
https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...
- alpine使用的避坑指南
alpine,是一个重量仅为5 MB的最小Linux发行版.它还有基本的linux工具和一个不错的包管理器APK.APK非常稳定,有相当数量的包.由于体积小,在容器中很受欢迎,但是使用上坑也很多,大部 ...
- java DES转C#DES加密解密
一个程序用到java的cn.core.jar加密的,需要在.NET 中解密,发现JAVA的des算法与C#的有点区别. 自己不太懂加密解密算法,所以找了个省事的方法,用IKVM.NET,用这个将cn. ...
- php配置出错,引发上传文件出错
今天在做文件上传的时候,按正常逻辑提交,可提交到服务器后,$_FILES['tmp_name']死活不对,表单的enctype="multipart/form-data" 这个也加 ...