Created
March 27, 2019 14:10
-
-
Save MateusAndrade/eaecfb27d79390a50d2d26e8c0f834cb to your computer and use it in GitHub Desktop.
Converts a string monetary value to a integer based on cents
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
/** | |
* Converts a string monetary value 9,00 to a integer | |
* @param {String} stringValue | |
*/ | |
const stringMoneyToCents = (stringValue) => { | |
const regexCleanStr = new RegExp( | |
/^[0-9]*$/g, | |
); | |
const regexRemoveDots = new RegExp( | |
/[,.]/g, | |
); | |
if (typeof stringValue === 'string') { | |
return parseInt( | |
stringValue | |
.replace(regexCleanStr, '') | |
.replace(regexRemoveDots, ''), | |
10, | |
); | |
} | |
return stringValue; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment