Skip to content

Instantly share code, notes, and snippets.

View mathiasleroy's full-sized avatar

Mathias Leroy mathiasleroy

View GitHub Profile
@Kobold
Kobold / round.js
Created September 16, 2015 04:05
Cool intelligent rounding function
function roundToSignificantFigures(num, sigFigs, truncationFunc) {
if (num == 0) {
return 0;
}
// We use base 5 because it makes for pretty numbers without intervals
// quite as large as base 10.
var d = Math.ceil(Math.log(num < 0 ? -num: num) / Math.log(5));
var power = sigFigs - d;