javascript - What does the following snippet do with num? -
i read other's code, there piece of code below. wondering method num?
formatnumber: function (num, digit) { var pow = math.pow(10, digit || 5); return math.round(num * pow) / pow; } btw when running formatnum(11.267898, 5), gave me 11.2679, ok?
essentially, function returns number precision. precision digit, 5 if not provided.
the return part brings many values (equal digit) decimal right left , discard rest , divides again original value reduces precision of digit.
regarding btw edit -
the value obtained correct. see details below
when call formatnum(11.267898, 5), you're asking number round 5 digit precision , number has 6 digit precision - precision digits after dot.
now when call num * pow number becomes 1126789.8 , when round number, rounds closest integer 11.26790. when divide pow (100000), number becomes 11.2679, discarding last 0 trailing 0 in precision pointless.
Comments
Post a Comment