Created
October 3, 2018 19:01
-
-
Save raulingg/3ceba329618893f48c4b260d6ed6b48d to your computer and use it in GitHub Desktop.
Handle money in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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