Rounding and Formatting Numbers
Video
JavaScript Notes
JavaScript
// round.js
// Math.round( ), floor( ), ceil( ) – returns Number
// Number.toFixed( ), to Exponential( ), toPrecision( ) – returns String
// Number.parseInt( ), parseFloat( ) – returns Number
//
let number1 = 1111.567;
let str1 = "1111.567 tonnes of apple sauce";
let dec1 = 0.000161;
Math.round(number1); // returns to closest integer, 1112
Math.floor(number1); // returns to previous integer, 1111
Math.ceil(number1); // returns to next integer, 1112
number1.toFixed(4); // returns to 4 d.p., 1111.5670
number1.toExpontential(2); // returns to 1.11e+3
number1.toExpontential(6); // returns to 1.111567e+3
dec1.toPrecision(1); // returns 0.0002 (looks at the first non-zero significant digit)
parseInt(str1); // returns 1111
parseFloat(str1); // returns 1111.567
Feedback
Submit and view feedback