Math#roundTo()

roundTo(value, place, base) → {number}

Round to some place comparative to a base, default is 10 for decimal place.
The place is represented by the power applied to base to get that place.

e.g. 2000/7 ~= 285.714285714285714285714 ~= (bin)100011101.1011011011011011

roundTo(2000/7,3) === 0
roundTo(2000/7,2) == 300
roundTo(2000/7,1) == 290
roundTo(2000/7,0) == 286
roundTo(2000/7,-1) == 285.7
roundTo(2000/7,-2) == 285.71
roundTo(2000/7,-3) == 285.714
roundTo(2000/7,-4) == 285.7143
roundTo(2000/7,-5) == 285.71429

roundTo(2000/7,3,2)  == 288       -- 100100000
roundTo(2000/7,2,2)  == 284       -- 100011100
roundTo(2000/7,1,2)  == 286       -- 100011110
roundTo(2000/7,0,2)  == 286       -- 100011110
roundTo(2000/7,-1,2) == 285.5     -- 100011101.1
roundTo(2000/7,-2,2) == 285.75    -- 100011101.11
roundTo(2000/7,-3,2) == 285.75    -- 100011101.11
roundTo(2000/7,-4,2) == 285.6875  -- 100011101.1011
roundTo(2000/7,-5,2) == 285.71875 -- 100011101.10111

Note what occurs when we round to the 3rd space (8ths place), 100100000, this is to be assumed
because we are rounding 100011.1011011011011011 which rounds up.

Parameters
Name Type Argument Default Description
value number

The value to round.

place number <optional>
0

The place to round to.

base number <optional>
10

The base to round in. Default is 10 for decimal.

Returns
number -

The rounded value.

Source code: math/Math.js (Line 235)
doc_phaser
2017-02-14 10:55:02
Comments
Leave a Comment

Please login to continue.