Skip to content

Instantly share code, notes, and snippets.

@morozVA
Created January 3, 2018 09:47
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 morozVA/988ff551f8b88c9709f6c56eeed20d7b to your computer and use it in GitHub Desktop.
Save morozVA/988ff551f8b88c9709f6c56eeed20d7b to your computer and use it in GitHub Desktop.
js number_format
/***
number - исходное число
decimals - количество знаков после разделителя
dec_point - символ разделителя
thousands_sep - разделитель тысячных
***/
function number_format(number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function(n, prec) {
var k = Math.pow(10, prec);
return '' + (Math.round(n * k) / k)
.toFixed(prec);
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
.split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '')
.length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1)
.join('0');
}
return s.join(dec);
}
// Пример 1: number_format(1234.56);
// Результат: '1,235'
// Пример 2: number_format(1234.56, 2, ',', ' ');
// Результат: '1 234,56'
// Пример 3: number_format(1234.5678, 2, '.', '');
// Результат: '1234.57'
// Пример 4: number_format(67, 2, ',', '.');
// Результат: '67,00'
// Пример 5: number_format(1000);
// Результат: '1,000'
// Пример 6: number_format(67.311, 2);
// Результат: '67.31'
// Пример 7: number_format(1000.55, 1);
// Результат: '1,000.6'
// Пример 8: number_format(67000, 5, ',', '.');
// Результат: '67.000,00000'
// Пример 9: number_format(0.9, 0);
// Результат: '1'
// Пример 10: number_format('1.20', 2);
// Результат: '1.20'
// Пример 11: number_format('1.20', 4);
// Результат: '1.2000'
// Пример 12: number_format('1.2000', 3);
// Результат: '1.200'
// Пример 13: number_format('1 000,50', 2, '.', ' ');
// Результат: '100 050.00'
// Пример 14: number_format(1e-8, 8, '.', '');
// Результат: '0.00000001'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment