Created
April 23, 2018 13:54
-
-
Save alanleite/3e260ab7694e0594ee730e2996dab73d to your computer and use it in GitHub Desktop.
Format real
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
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