Skip to content

Instantly share code, notes, and snippets.

@MateusAndrade
Created March 27, 2019 14:10
Show Gist options
  • Save MateusAndrade/eaecfb27d79390a50d2d26e8c0f834cb to your computer and use it in GitHub Desktop.
Save MateusAndrade/eaecfb27d79390a50d2d26e8c0f834cb to your computer and use it in GitHub Desktop.
Converts a string monetary value to a integer based on cents
/**
* 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