Javascript: Cant get a round number -


i tried cant seem final result round number 2 decimal digits. it's standard deviation

thank in advance, here code.

function round(number) {   var final = math.round(number * 100) / 100;   return final; } function stdev(arr) {   var avg = average(arr);   var squarediff = arr.map(function(arr) {     var diff = arr - avg;     var sqrdiff = diff * diff;     return sqrdiff;   });   var avgsquarediff = average(squarediff);   var stddev = math.sqrt(avgsquarediff);   return stddev;   function average(data) {     var sum = data.reduce(function(sum, arr) {       return sum + arr;     }, 0);     return round(sum / data.length);   } } 

you round value when return in average.

however, in stdev, calculate square root of rounded value.

that operation return number lots of decimals, , return directly without rounding again.

most need

function round(number) {   var final = math.round(number * 100) / 100;   return final; } function average(data) {   var sum = data.reduce(function(sum, arr) {     return sum + arr;   }, 0);   return sum / data.length; } function stdev(arr) {   var avg = average(arr);   var squarediff = arr.map(function(arr) {     var diff = arr - avg;     var sqrdiff = diff * diff;     return sqrdiff;   });   var avgsquarediff = average(squarediff);   var stddev = math.sqrt(avgsquarediff);   return round(stddev); } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -