Created
February 10, 2019 09:05
-
-
Save AlekVolsk/16f62677e713077dd652927ed79de8e4 to your computer and use it in GitHub Desktop.
Форматирование числа на js
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
/* | |
num number | |
thSeparator thousands separator | |
floatSeparator decimal separator | |
floatFixed fixed number of digits after the decimal point | |
*/ | |
function numberFormat(num, thSeparator, floatSeparator, floatFixed) | |
{ | |
var n; | |
if (!thSeparator) { thSeparator = ''; } | |
if (floatFixed) { n = num.toFixed(floatFixed); } else { n = num.toString(); } | |
if (floatSeparator) { n = n.replace(".", floatSeparator); } | |
return n.replace(/(\d{1,3}(?=(?:\d\d\d)+(?!\d)))/g, "$1" + thSeparator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment