# Khan/khan-exercises

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

84 lines (74 sloc) 4.041 kb
 Rounding numbers
shuffle( [1, 2, 3, 4, 5, 6, 7, 8, 9], 5 ) +DIGITS.join( "" ) randFromArray([ -2, -3 ]) placesLeftOfDecimal[ -PLACE ] roundTo( PLACE, NUM )

Round commafy( NUM ) to the nearest TPLACE.

ROUNDED

1st way: Look at the placesLeftOfDecimal[ -1 - PLACE ]s digit DIGITS[ 5 + PLACE ] to see whether to round up or down.

Because it is more thanequal to 5, we round up, giving ROUNDED.

Because it is less than 5, we round down, giving commafy( ROUNDED ).

2nd way: Consider which end of the number line is closer to NUM

init({ range: [ [ -0.05 *pow( 10, -PLACE ) , 1.3*pow( 10, -PLACE ) ], [-1, 1] ], scale: [ 600 * pow( 10, PLACE ), 40 ] }); numberLine( floorTo( PLACE , NUM ), ceilTo( PLACE , NUM ) + pow( 10, -( PLACE+2 )) , round( pow( 10, -(PLACE + 1 ))) ); style({ stroke: "#FFA500", fill: "#FFA500", strokeWidth: 3.5, arrows: "->" }); ellipse( [ 10 * pow( 10, -( PLACE+1 )) * (NUM - floorTo( PLACE , NUM ))/pow( 10,- (PLACE) ), 0 ], [pow( 10, -PLACE ) / 100, 0.15]); label( [ 10 * pow(10, -( PLACE+1 )) * ( NUM - floorTo( PLACE , NUM ))/pow( 10,- (PLACE) )], NUM, "below");
shuffle( [1, 2, 3, 4, 5, 6, 7, 8, 9], 6 ) +DIGITS.join( "" ) / 10000 randFromArray([ 0, 1, 2 ]) placesRightOfDecimal[ PLACE ] roundTo( PLACE, NUM )

Round commafy( NUM ) to the nearest TPLACE.

ROUNDED