返回函数数组中的2个小数位

时间:2018-04-01 01:35:31

标签: javascript return decimal

我想将一个项目数组返回到2位小数。

示例 - 返回5表示“输入年份”,1000表示“输入iniInvest”,10表示“输入兴趣”,1000表示“输入年份”:  的 1100,2310,3641,5105.1,6715.610000000001

我希望数组项以小数点后两位结尾。 1100.00,2310.00,3641.00,5105.10,6715.61

我尝试过一些事情,包括

return totalArry.toFixed(2);
return totalArry[].toFixed(2);

这些都没有解决问题。我已经做了一些搜索,但我不太了解如何转换我发现的一些答案来处理我的代码。

function investCalc() {
    for(i=1; i<years; i++) {
        totalYear += parseFloat(yearlyInv); 
        totalInt = parseFloat(totalYear) * parseFloat(interest/100); 
        totalYear += parseFloat(totalInt); 
        totalArry.push(totalYear);
    }
    return totalArry.toFixed(2);
}
var years = prompt("Enter years");
var iniInvest = prompt("Enter iniInvest");
var interest = prompt("Enter interest");
var yearlyInv = prompt("Enter yearlyInv");
var totalInt = parseFloat(iniInvest) * parseFloat(interest/100); 
var totalYear = parseFloat(iniInvest) + parseFloat(totalInt); 
var totalArry = [totalYear]; 

alert(investCalc());

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

如果希望数组的每个元素都有2个小数位,则需要在每个元素上调用.toFixed(2),而不是在数组变量上调用totalArry.push(totalYear.toFixed(2));。您可以在将元素推送到数组之前执行此操作:toFixed

请注意totalArry.push(+totalYear.toFixed(2));返回一个字符串,因此您将获得一个字符串数组。如果您想要一组数字,可以使用unary plus operatorfunction investCalc() { for(let i = 1; i < years; i++) { totalYear += parseFloat(yearlyInv); totalInt = parseFloat(totalYear) * parseFloat(interest/100); totalYear += parseFloat(totalInt); totalArry.push(+totalYear.toFixed(2)); } return totalArry; } var years = prompt("Enter years"); var iniInvest = prompt("Enter iniInvest"); var interest = prompt("Enter interest"); var yearlyInv = prompt("Enter yearlyInv"); var totalInt = parseFloat(iniInvest) * parseFloat(interest/100); var totalYear = parseFloat(iniInvest) + parseFloat(totalInt); var totalArry = [+totalYear.toFixed(2)]; console.log(investCalc());

&#13;
&#13;
Debugger listening on ws://127.0.0.1:39061/61f6ad51-d255-463a-a0ad-b686be9b8b0d
Object {dataReducer: Object, reducerSPY: Object, userApp: Object}
home.js:31
Error: Cannot find module 'StyleSheet'
module.js:555
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.get StyleSheet [as StyleSheet] (c:\AndroidEverything\AndroidStudioProjects\Property2\AwesomeProject\node_modules\react-native\Libraries\react-native\react-native-implementation.js:98:29)
    at Object.<anonymous> (c:/AndroidEverything/AndroidStudioProjects/Property2/AwesomeProject/app/components/home.js:195:16)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要将要推入数组的浮点数限制为两个小数位而不是整个数组。这是代码。

function investCalc() {
for(i=1; i<years; i++) {
    totalYear += parseFloat(yearlyInv); 
    totalInt = parseFloat(totalYear) * parseFloat(interest/100); 
    totalYear += parseFloat(totalInt); 
    totalYear = parseFloat(totalYear).toFixed(2); //round or pad with zeros to two decimal places
    totalArry.push(totalYear);                   //Then push into the array.
    }
return totalArry;
 }
}
var years = prompt("Enter years");
var iniInvest = prompt("Enter iniInvest");
var interest = prompt("Enter interest");
var yearlyInv = prompt("Enter yearlyInv");
var totalInt = parseFloat(iniInvest) * parseFloat(interest/100); 
var totalYear = parseFloat(iniInvest) + parseFloat(totalInt); 
totalYear = totalYear.toFixed(2);
var totalArry = [totalYear]; 

alert(investCalc());