Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created September 21, 2009 00:14
Show Gist options
  • Save jfsiii/189995 to your computer and use it in GitHub Desktop.
Save jfsiii/189995 to your computer and use it in GitHub Desktop.
Floating point numbers not strings
function getFixed(mixed, precision)
{
mixed = '' + mixed;
precision = precision || 2;
var matches = mixed.match(/\d+\.?\d+/),
amount = (matches && matches[0]) || null,
fixed = parseFloat(amount).toFixed(precision);
return parseFloat(fixed);
}
Number.prototype.asFixed = function(precision)
{
return getFixed(this, precision);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment