Skip to content

Instantly share code, notes, and snippets.

@Warrenn
Created October 22, 2019 08:41
Show Gist options
  • Save Warrenn/00d0d3fea03cb9ea62982b3511e55c26 to your computer and use it in GitHub Desktop.
Save Warrenn/00d0d3fea03cb9ea62982b3511e55c26 to your computer and use it in GitHub Desktop.
show currency
showCurrency(value: number, precision: number, seperator: string, decimalSeperator: string): string {
const stringValue = `${value || 0}`
const parts = stringValue.split('.')
let numeric = `${parts[0]}`
if (seperator) numeric = numeric.replace(/(\d)(?=(\d{3})+(?!\d))/g, `$1${seperator}`)
let decimal = `${parts[1] || 0}`
if (precision > 0) decimal = decimal.padEnd(precision, '0')
decimalSeperator = decimalSeperator || '.'
return `${numeric}${decimalSeperator}${decimal}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment