Skip to content

Instantly share code, notes, and snippets.

@joemaffei
Created September 6, 2018 17:56
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 joemaffei/c0db2a84c7365579702393037ff627b3 to your computer and use it in GitHub Desktop.
Save joemaffei/c0db2a84c7365579702393037ff627b3 to your computer and use it in GitHub Desktop.
Converts a number into dollars the "hard" way, because toLocaleString is unsupported by react-native on Android
const formatDollarAmount = (num) => (
'$' + `${Math.floor(num)}`
.split('')
.reverse()
.reduce((result, char, index) => {
index > 0 && !(index % 3) && result.push(',');
return [...result, char]
}, [])
.reverse()
.join('') + `${(num % 1).toFixed(2)}`.substr(1)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment