I am writing code to round to six decimal places after some arithmetic. I am looping through the contents of an array and finding out the contents of the array. Then I divide it by the array length. I found the function toFixed. I am setting toFixed(6). So for example. arraycontents/array.length.toFixed(6) Should get six places after the decimal. I am only getting 1?
array = [1, 1, 0, -1, -1];
var positive_count = 0;
var negative_count = 0;
var zero_count = 0;
function plusMinus(array) {
for(var i = 0; i < array.length; i++) {
if(array[i] > 0) {
positive_count++;
//console.log("Positive Count " + positive_count);
} else if (array[i] < 0) {
negative_count++;
//console.log("Negative Count " + negative_count);
} else if (array[i] == 0) {
zero_count++;
// console.log("Zero count " + zero_count);
}
}
var calculatePos = positive_count/array.length.toFixed(6);
calculatePos.toFixed(6);
console.log(calculatePos);
var calculateNeg = negative_count/array.length.toFixed(6);
console.log(calculateNeg);
var calculateZero = zero_count/array.length.toFixed(6);
console.log(calculateZero);
}
plusMinus(array);
I am writing code to round to six decimal places after some arithmetic. I am looping through the contents of an array and finding out the contents of the array. Then I divide it by the array length. I found the function toFixed. I am setting toFixed(6). So for example. arraycontents/array.length.toFixed(6) Should get six places after the decimal. I am only getting 1?
array = [1, 1, 0, -1, -1];
var positive_count = 0;
var negative_count = 0;
var zero_count = 0;
function plusMinus(array) {
for(var i = 0; i < array.length; i++) {
if(array[i] > 0) {
positive_count++;
//console.log("Positive Count " + positive_count);
} else if (array[i] < 0) {
negative_count++;
//console.log("Negative Count " + negative_count);
} else if (array[i] == 0) {
zero_count++;
// console.log("Zero count " + zero_count);
}
}
var calculatePos = positive_count/array.length.toFixed(6);
calculatePos.toFixed(6);
console.log(calculatePos);
var calculateNeg = negative_count/array.length.toFixed(6);
console.log(calculateNeg);
var calculateZero = zero_count/array.length.toFixed(6);
console.log(calculateZero);
}
plusMinus(array);
Share
Improve this question
asked Jun 22, 2022 at 16:16
mustyg123mustyg123
371 silver badge5 bronze badges
2
-
1
toFixed()
doesn't modify the value in place, you need to assign the result.calculatePos = calculatePos.toFixed(6);
– Barmar Commented Jun 22, 2022 at 16:19 - Thank you sir. I think this is another question of mine you have answered. Thank you for your help. – mustyg123 Commented Jun 22, 2022 at 16:25
1 Answer
Reset to default 4Let me quickly explain what is happening in your code logic:
array.length // 5
positive_count = 2;
negative_count = 2;
zero_count = 1;
var calculatePos = positive_count/array.length.toFixed(6); // 2 / 5.toFixed(6) the result should be an error.
var calculateNeg = negative_count/array.length.toFixed(6); // 2 / 5.toFixed(6) the result should be an error.
var calculateZero = zero_count/array.length.toFixed(6); // 0 / 1.toFixed(6) the result should be an error.
What you should do:
var calculatePos = (positive_count/array.length).toFixed(6); // => '0.400000' string
var calculateNeg = (negative_count/array.length).toFixed(6); // => '0.400000' string
var calculateZero = (zero_count/array.length.toFixed(6); // => '0.000000' string
If you wish to convert the types to a number, you can do it with parseFloat(string)
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745461894a4628745.html
评论列表(0条)