# Khan/khan-exercises

Fetching contributors…
Cannot retrieve contributors at this time
93 lines (86 sloc) 5.49 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 - this end is circled in blue.

init({ range: [ [ -0.06 * 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 }); 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"); style({ stroke: "#6495ED", "fill": "none", strokeWidth: 3.5 }); if( DIGITS[ 5 + PLACE ] >= 5 ) { ellipse( [ pow( 10, -PLACE ), -0.55 ], [pow( 10, - PLACE - 0.5 ) / 6.5, 0.35] ); } else { ellipse( [ 0, -0.55 ], [pow( 10, - PLACE - 0.5 ) / 6.5, 0.35] ); }
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