我正在使用VueJS创建固定资产折旧计算器。问题在于计算双倍余额和列出结果的表格,显示年份,每年的折旧金额,资产的有用年限和每年的账面价值。我想要这样的东西: 购买价格= 25000 有用的年份= 5 **
Year| Depreciated Amount| Book Value
1 | 9000 |16000
2 | 6000 |10000
**
资产购买价格和有用年份由用户动态提供。就像我上面的例子一样,循环应该运行5次(5年有用)。
这是我的代码:
reducingBalDepCal()
{
//Calculates asset depreciation based on declining balance
//hide previous results if any
this.showResults = false;
//This is the historical cost of the fixed asset. The value is being supplied by the user.
var purchasePrice = this.reducingbal.purchase_price;
//The number of years the asset will be useful to the business. Its the lifespan of the asset in the operational business. The vulue is being supplied by the user.
var usefulYears = this.reducingbal.useful_years;
//is calculated by dividing 100% with asset useful years.
var rate = (100/usefulYears);
//Then the rate is doubled
var doubleDepreciationRate = rate*2;
var depAmount = []; //This array holds depraciated amount each year. i.e current book value-current depreciated amount
var writenDownValue = []; //This array holds the current book value in each depreciation iteration
//The for loop calaculates the depreciation in each year depending on the number of useful years and stores current depreciated amount in the depAmount array and the current writen down amount (book value) in the writenDownValue array.
for(var i=usefulYears+1;i>1;i-=1)
{
writenDownValue.push(purchasePrice-=(doubleDepreciationRate/100)*purchasePrice);
depAmount.push(purchasePrice-writenDownValue[i]);
}
//Assigns the results on data variable
//this.reducingBalProjection =depAmount;
this.showReducingBalProjection = true;
console.log(writenDownValue);
console.log(depAmount);
},
这是depAmount数组的结果:
(50) [15000, 9000, 5400, 3240, 1944, 1166.4, 699.84, 419.904, 251.9424, 151.16544, 90.699264, 54.4195584, 32.65173504, 19.591041024, 11.754624614399999, 7.052774768639999, 4.231664861183999, 2.5389989167103995, 1.5233993500262397, 0.9140396100157437, 0.5484237660094462, 0.3290542596056677, 0.1974325557634006, 0.11845953345804036, 0.07107572007482421, 0.04264543204489453, 0.025587259226936717, 0.01535235553616203, 0.009211413321697217, 0.00552684799301833, 0.003316108795810998, 0.0019896652774865986, 0.0011937991664919593, 0.0007162794998951756, 0.00042976769993710535, 0.0002578606199622632, 0.00015471637197735793, 0.00009282982318641476, 0.00005569789391184885, 0.00003341873634710931, 0.000020051241808265585, 0.00001203074508495935, 0.0000072184470509756104, 0.000004331068230585366, 0.0000025986409383512197, 0.0000015591845630107317, 9.35510737806439e-7, 5.613064426838634e-7, 3.36783865610318e-7, 2.020703193661908e-7]
结果为writenDown数组:
[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, -0.017058172817957813, -0.05572336453866218, -0.10924812013634315, -0.19190570777038227, -0.3257381508098567, -0.5464341007319596, -0.9128458108492518, -1.5226830705263446, -2.5385691490104625, -4.231407000564037, -7.052620052268022, -11.754531784576812, -19.590985326106086, -32.65170162126365, -54.41953834875819, -90.69925196925492, -151.16543278155294, -251.94239566893177, -419.90399740135905, -699.8399984408155, -1166.3999990644893, -1943.9999994386935, -3239.999999663216, -5399.999999797929 ]
两个结果在语义上都是错误的。我希望循环运行usefulyears fold。但是这两个循环都没有达到我的预期,可能正在按照我所说的去做。我无法弄清楚如何才能达到预期的效果。
另一个担心是如何打包do数组并将其打印出来,如下所示:
Year| Depreciated Amount| Book Value
1 | 9000 |16000
2 | 6000 |10000
请帮助。
答案 0 :(得分:0)
如果你得到两个长度50
的数组,那么this.reducingbal.useful_years
设置不正确的主要问题不是吗?
无论如何,实施here所述的计算给出的结果与9000
和6000
的前两年不同:
(function() {
// A table row
const Row = (nr, start, apdr) => ({
year: nr,
startValue: start,
depExpense: start * apdr,
endValue: start - (start * apdr)
})
// The calculation
const decliningBalanceDepreciation = multiplier => (
startValue,
endValue,
years
) => {
const valueDecrease = startValue - endValue;
// Applicable Percentage Depreciation Rate
const apdr = (valueDecrease / years) / valueDecrease * multiplier;
const rows = [
Row(1, startValue, apdr)
];
for (let i = 1; i < years; i += 1) {
rows.push(
Row(i + 1, last(rows).endValue, apdr)
)
}
return rows;
};
const dblDecBalDep = decliningBalanceDepreciation(2);
console.log(
dblDecBalDep(25000, 0, 5)
);
function last(arr) { return arr[arr.length - 1]; }
}())