英文:
Excel calculation in PHP
问题
I am working on a table where some of the data is coming back directly from an api and some of it is calculated. I have a Google Spreadsheets sheet as an example for different values and am having some trouble translating the formulas used to PHP code.
Example of a row in spreadsheets:
A18 | B18 | C18 | E18 | H18 |
---|---|---|---|---|
5000 | 91 | 9.7 | 72 | 0.7740 |
Note that this is just one example.
- The values I know in my PHP because I get them back from the api are A18, C18, and E18
- H18 is calculated by the Excel formula
=AFRONDEN(((((1+(C18))^(1/12))-1)*100*1000)/1000,3)
. I managed this in PHP, and the value is saved in a variable. - I am trying to calculate B18, where the outcome should be 91 for this example. The formula as it is in Google Sheets is
=-BET(H18/100,E18,A18)
I tried to use different ways of calculation but ended up with really weird numbers. Also tried to investigate PMT instead of BET but with no success so far. Does someone know how to approach this?
英文:
I am working on a table where some of the data is comming back directly from an api and some of it is calculated. I have an google spreadsheets sheet as an example for diffrent values and am having some trouble translating the formulas used to PHP code.
Example of a row in spreadsheets:
A18 | B18 | C18 | E18 | H18 |
---|---|---|---|---|
5000 | 91 | 9.7 | 72 | 0.7740 |
Note that this is just one example.
- The values I know in my PHP because I get them back from the api are A18, C18 and E18
- H18 is calculated by excel formula
=AFRONDEN(((((1+(C18))^(1/12))-1)*100*1000)/1000,3)
. I managed this in PHP and the value is saved in a variable - I am trying to calculate B18, where the outcome should be 91 for this example. The formula as it is in google sheets is
=-BET(H18/100,E18,A18)
I tried to use diffrent ways of calculation but ended up with really weird numbers, Also tried to investigate PMT instead of BET but with no success so far. Does someone know how to approach this?
答案1
得分: 1
根据 PMT,根据我所看到的,你的计算在 PHP 中应该如下:
A18 * H18/100 * pow(1 + H18/100, E18) / (pow(1 + H18/100, E18) - 1)
这不是四舍五入的。如果需要的话,可以使用 round()
。
附带说明:如果你打算在财务数值的真实应用中使用此计算,请记住除法操作将导致浮点值,这不始终是精确的。
英文:
As far as I see based on PMT, your calculation should be like this in PHP:
A18 * H18/100 * pow(1 + H18/100, E18) / (pow(1 + H18/100, E18) - 1)
This is not rounded. If you need to, use round().
Side note: If you want to use this for a real application with financial values, please keep in mind that the division operation will result in a float-value, which is not always precise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论