Skip to content

Instantly share code, notes, and snippets.

@wesdeveloper
Created May 25, 2018 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesdeveloper/3c340891ef9e07edebafe0548174706b to your computer and use it in GitHub Desktop.
Save wesdeveloper/3c340891ef9e07edebafe0548174706b to your computer and use it in GitHub Desktop.
const ThousandsSeparatorRegex = () => {
const format = (value, separator = ',') => {
const formatToThousands = (str, formater = ',') => str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, formater);
if (value >= 0 || (value.length >= 0 && value.length <= 15)) {
const formatedThousands = formatToThousands(value, separator);
return formatedThousands;
} else if (value < 0) {
const formatedThousands = formatToThousands(value, separator);
return formatedThousands;
}
return '0';
};
return { format };
};
export default ThousandsSeparatorRegex;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment