简体   繁体   English

Excel ROUND function in Javascript

[英]Excel ROUND function in Javascript

I've got a mathematical formula in Excel, which is the following:我在 Excel 中有一个数学公式,如下所示:

ROUND((B2+C2)*(B55/100)/12;2)

Initial values:初始值:

  • B2 = 1000 B2 = 1000
  • C2 = 0 C2 = 0
  • B55 = 0,03 B55 = 0,03

Results (t means time in months).结果(t 表示以月为单位的时间)。 来自excel的值

Here is my Javascript approach:这是我的 Javascript 方法:

 (function _calculateRates() { var singlePayment = parseInt(1000, 10), amount = singlePayment, monthlyPayment = parseInt(0, 10), investTime = parseFloat(12), rate_a = parseFloat(0.03), rate_b = parseFloat(0.03), investment = monthlyPayment, interest = 0; for (var month = 0; month < investTime; month += 1) { investment = (month === 0)? 0: monthlyPayment; interest = Number(((amount + investment) * (rate_a / 100) / 12).toFixed(2)); amount = Number((amount + interest + investment).toFixed(2)); } console.log('Result: ', amount); })();

As one can see, the result is not correct.可以看出,结果是不正确的。

Where can I find the Microsoft Excel algorithm for ROUND()?在哪里可以找到 ROUND() 的 Microsoft Excel 算法?

In Excel =0.3/12 evaluates to 0.025 . 在Excel中=0.3/12计算0.025 So rounded to 2 decimals it is 0.03 . 因此,四舍五入为两位小数是0.03

In JavaScript var result = 0.3/12; 在JavaScript中, var result = 0.3/12; results in 0.024999999999999998 . 结果为0.024999999999999998 That .toFixed(2) is 0.02 . .toFixed(2)0.02

Internally Excel also gets 0.024999999999999998 like all systems using IEEE Standard for Floating-Point Arithmetic (IEEE 754) . 与使用IEEE浮点算术标准(IEEE 754)的所有系统一样, Excel内部也会获得0.024999999999999998 But it has the additional rule, to only take 15 digits maximum. 但是它还有一条附加规则,最多只能输入15位数字。 That is 0.02499999999999 + 0.000000000000009 , which is 0.025 . 0.02499999999999 + 0.000000000000009 ,即0.025

So we cannot using .toFixed in JavaScript . 因此,我们无法在JavaScript使用.toFixed If we are using another method for rounding in JavaScript the this leads to the same result as in Excel . 如果我们使用另一种方法在JavaScript进行四舍五入,则会得到与Excel相同的结果。

See example using simple values: 请参阅使用简单值的示例:

 var result = 0.3/12; console.log(result); console.log(result.toFixed(2)); console.log(Math.floor((Math.pow(10, 2)*result)+0.5)*Math.pow(10, -2)); 

See example using your algorithm: 请参阅使用算法的示例:

 (function _calculateRates() { var singlePayment = parseInt(1000, 10), amount = singlePayment, monthlyPayment = parseInt(0, 10), investTime = parseFloat(12), rate_a = parseFloat(0.03), rate_b = parseFloat(0.03), investment = monthlyPayment, interest = 0; for (var month = 0; month < investTime; month += 1) { investment = (month === 0) ? 0 : monthlyPayment; interest = Number(((amount + investment) * (rate_a / 100) / 12)); interest = Math.floor((Math.pow(10, 2)*interest)+0.5)*Math.pow(10, -2); amount = Number((amount + interest + investment)); amount = Math.floor((Math.pow(10, 2)*amount)+0.5)*Math.pow(10, -2); } console.log('Result: ', amount); })(); 

Because it is related to this question, especially why in JavaScript var result = 0.3/12; 因为它与此问题有关,尤其是为什么在JavaScript var result = 0.3/12; results in 0.024999999999999998 , the link to What Every Programmer Should Know About Floating-Point Arithmetic could be helpful. 结果为0.024999999999999998每个程序员应了解的有关浮点算法的链接可能会有所帮助。

I writed a script to round numbers in JavaScript like Excel or Google Sheets.我编写了一个脚本来对 JavaScript 中的数字进行舍入,例如 Excel 或 Google 表格。 The code is avaliable in my GitHub repository:https://github.com/marcos-nonaka/round-numbers-like-excel-in-js该代码在我的 GitHub 存储库中可用:https://github.com/marcos-nonaka/round-numbers-like-excel-in-js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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