Created
December 15, 2020 16:39
-
-
Save Erushenko/cd8efb3088f4294ae86c8e1f00e2fddc to your computer and use it in GitHub Desktop.
format money
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
function formatMoney(value) { | |
const preparedValue = | |
typeof value === 'undefined' || value === null ? '' : value.toString() | |
let [dollars, cents = ''] = preparedValue.replace(/[^0-9.-]/g, '').split('.') | |
dollars = dollars.replace(/^0+/, '') || '0' | |
cents = `${cents}00`.slice(0, 2) | |
return `${dollars}.${cents}`.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment