Skip to content

Instantly share code, notes, and snippets.

@joates
joates / round_floats.js
Last active May 17, 2016 23:16
rounding float values in javascript can produce undesirable results (i.e. errors in the exponent), although certain situations can still produce inaccuracy, this fixes the common cases, ** use with caution **
// fixes rounding of float values in javascript
Number.prototype.round = function(num_places) {
return +(Math.round(this +'e+'+ num_places) +'e-'+ num_places)
}