javascript - Math.flooring a scientific notation at a certain decimal -
i trying math.floor scientific notation, @ 1 point number gets big , current method doesn't work anymore. using atm
var nr = (number+"").length - 4; if( nr > 1 ) { nr = math.pow( 10, nr ); number= math.floor(number/nr)*nr; number= number.toexponential(3); }
when becomes scientific notation default, think that's e20+, .length method doesn't work anymore since length returns isn't accurate. can think of work around, , that's find out number after e, , update nr math.floor properly, seems work simple. here's example number 8.420960987929105e+79
want turn 8.420e+79
, there way can math.floor third decimal point always, no matter number is? stands when use toexponential(3) rounds number. numbers can high e+200 easily, need easier way of doing i'm doing.
edit: managed find work around works besides connor peet's answer wants options
var nr = 8.420960987929105e+79+""; var nr1 = nr.substr(0,4); var nr2 = nr.substr(4, nr.length); var finalnr = number(nr1 + 0 + nr2).toexponential(3);
this way more of hack, adds 0 after 4th number when toexponential rounds up, gets 'floored' pretty much.
i wrote little snippet round number number of significant figures time ago. might find useful
function sigfigs(num, figures) { var delta = math.pow(10, math.ceil(math.log(num) / math.log(10)) - figures); return math.round(num / delta) * delta; } sigfigs(number, 3); // => 8.420e+79
Comments
Post a Comment