Created
February 6, 2020 03:57
-
-
Save warrenlalata/08be47d995ed6a56c98dbe0293014668 to your computer and use it in GitHub Desktop.
Mask String with Thousand Separator
This file contains 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
/** | |
* Masks Input with thousand separator | |
* | |
* @param {string} amount - Amount | |
* @return {string} Masked Amount | |
* | |
* @example | |
* formatAmount("1000") // 1,000 | |
*/ | |
export function formatAmount(amount) { | |
if (!amount) return; | |
return amount | |
.toString() | |
.replace(/,/g, "") | |
.replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
} | |
/** | |
* Normalizes Amount from Masked format | |
* | |
* @param {string} amount - Amount | |
* @return {string} Normalized Amount | |
* | |
* @example | |
* normalizeAmount("1,000") // 1000 | |
*/ | |
export function normalizeAmount(amount) { | |
return amount.toString().replace(/,/g, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment