Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Created February 10, 2019 09:05
Show Gist options
  • Save AlekVolsk/16f62677e713077dd652927ed79de8e4 to your computer and use it in GitHub Desktop.
Save AlekVolsk/16f62677e713077dd652927ed79de8e4 to your computer and use it in GitHub Desktop.
Форматирование числа на js
/*
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