Skip to content

Instantly share code, notes, and snippets.

@alanleite
Created April 23, 2018 13:54
Show Gist options
  • Save alanleite/3e260ab7694e0594ee730e2996dab73d to your computer and use it in GitHub Desktop.
Save alanleite/3e260ab7694e0594ee730e2996dab73d to your computer and use it in GitHub Desktop.
Format real
export function format(value, n, x, s, c) {
if (!value) return ''
var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
num = value.toFixed(Math.max(0, ~~n));
return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
};
export function formatReal(value) {
return format(value, 2, 3, '.', ',')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment