Skip to content

Instantly share code, notes, and snippets.

@raulingg
Created October 3, 2018 19:01
Show Gist options
  • Save raulingg/3ceba329618893f48c4b260d6ed6b48d to your computer and use it in GitHub Desktop.
Save raulingg/3ceba329618893f48c4b260d6ed6b48d to your computer and use it in GitHub Desktop.
Handle money in javascript
var formatDollarsToCents = function(value) {
value = (value + '').replace(/[^\d.-]/g, '');
if (value && value.includes('.')) {
value = value.substring(0, value.indexOf('.') + 3);
}
return !isNaN(value) ? Math.round(parseFloat(value) * 100) : 0;
}
var formatCentsToDollars = function(value) {
value = (value + '').replace(/[^\d.-]/g, '');
value = parseFloat(value);
return !isNaN(value) ? value / 100 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment