admin管理员组文章数量:1426648
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)
.
本文标签: cssHow do I get the decimal length to 6 decimal places in javascriptStack Overflow
版权声明:本文标题:css - How do I get the decimal length to 6 decimal places in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745461893a2659361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论