javascript - parseFloat.toPreceision just adds zeros -
i'm sending number/string 0.001 function below:
significantfigures = 4; function limitnumberofdigits(num) { var tempstr = ""; if (isnan(num)) return "\xd8"; else{ if (parsefloat(num) === 0 || (num.tostring().indexof('.') === -1 && parseint(num) < 9999) || num.tostring().length <= 4) { return num; } tempstr = parsefloat(num).toprecision(significantfigures); if (tempstr.indexof("e") > -1) { var starte = tempstr.indexof("e"); var ende = 0; (var = starte +2 ; < tempstr.length; i++ ) { // + ignore e , sign (+ or - ) if(parseint(tempstr[i], 10) > 0) { ende = i; }else { break; } } if (starte + 2 === ende) { var pow = tempstr[ende]; } else { var pow = tempstr.substring(starte +2 ,ende); } return tempstr.substring(0,starte) + "*10<sup>"+ pow +"</sup>"; }else { return parsefloat(num).toprecision(significantfigures); } } }
when im sending 0.2 or 0.11 im getting 0.2000 , 0.1100. issue here toprecision acts tofixed. ideas?
edit
what want? simple that, if numbers needs changed e.g 0.012312312041 should 0.0123 , numbers 0.12 or 28 should stay same.
Comments
Post a Comment