简体   繁体   English

将Excel公式转换为Javascript

[英]Convert Excel Formula to Javascript

I have this formula on excel 我在excel上有这个公式

=(1130000000000*F11^1.85)/(F19^1.85*(F13*(1-2/F15))^4.8655)

when 什么时候

F11 = q
F19 = 150 (Constant)
F13 = d
F15 = sdr

I convert it to this 我将其转换为此

Math.round((1130000000000*Math.pow(q,1.85))/(Math.pow(150,1.85)*Math.pow(d*(1-2/sdr)),4.8655))

but the results are wrong 但是结果是错误的

when 什么时候

q = 120
d = 200
sdr = 17

the result should be 8.76 but I am getting long numbers 结果应该是8.76但是我越来越长

any help ? 有什么帮助吗? Thanks 谢谢

From YUI blog 从YUI博客

JavaScript has a single number type: IEEE 754 Double Precision floating point. JavaScript具有单个数字类型:IEEE 754双精度浮点。 Having a single number type is one of JavaScript's best features. 具有单个数字类型是JavaScript的最佳功能之一。 Multiple number types can be a source of complexity, confusion, and error. 多种数字类型可能会导致复杂性,混乱和错误。 A single type is simplifying and stabilizing. 单一类型可以简化和稳定化。 Unfortunately, a binary floating point type has some significant disadvantages. 不幸的是,二进制浮点类型具有一些明显的缺点。 The worst is that it cannot accurately represent decimal fractions, which is a big problem because humanity has been doing commerce in decimals for a long, long time. 最糟糕的是它不能精确表示小数,这是一个大问题,因为人类很长一段时间以来一直在使用小数进行商业交易。 There would be advantages to switching to a binary-based number system, but that is not going to happen. 切换到基于二进制的数字系统将具有优势,但这不会发生。 As a consequence, 0.1 + 0.2 === 0.3 is false, which is the source of a lot of confusion. 结果,0.1 + 0.2 === 0.3是错误的,这是造成大量混乱的根源。

http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/ http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/

Completely ignore my previous answer, although it is a problem which you will definitely encounter should you continue with these large numbers, your actual issue is a misplaced bracket in your code. 完全忽略我以前的回答,尽管如果继续使用这些大数字肯定会遇到这个问题,但实际问题是代码中的括号放错了位置。 If you use this (simply replaced variable names with values): 如果使用此选项(用值简单替换变量名):

Math.round((1130000000000*Math.pow(120,1.85))/(Math.pow(150,1.85)*Math.pow((200*(1-2/17)),4.8655)))

It returns 9 (a rounded 8.76), the bracket you misplaced is around the 1-2 mark. 它返回9(四舍五入的8.76),放错位置的括号位于1-2标记附近。

For future reference, the largest number Javascript can comprehend without fault is +/- 9007199254740992. 供将来参考,无错误的Javascript最多可以理解为+/- 9007199254740992。

References: 参考文献:

What is JavaScript's highest integer value that a Number can go to without losing precision? 在不损失精度的情况下,Number可以使用的JavaScript的最高整数值是多少?

ECMA-262 - The Number Type ECMA-262-数字类型

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

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