Skip to content

Instantly share code, notes, and snippets.

@beardlessman
Created July 28, 2017 04:15
Show Gist options
  • Save beardlessman/1d668da5bf80df08506e913894431b9a to your computer and use it in GitHub Desktop.
Save beardlessman/1d668da5bf80df08506e913894431b9a to your computer and use it in GitHub Desktop.
разбивает цифры на порядки
$('.j-price').each(function () {
var newPrice = XFormatPrice($(this).text());
$(this).text(newPrice);
});
function XFormatPrice(_number)
{
var decimal=0;
var separator=' ';
var decpoint = '.';
var format_string = '# Р';
var r=parseFloat(_number)
var exp10=Math.pow(10,decimal);// приводим к правильному множителю
r=Math.round(r*exp10)/exp10;// округляем до необходимого числа знаков после запятой
rr=Number(r).toFixed(decimal).toString().split('.');
b=rr[0].replace(/(\d{1,3}(?=(\d{3})+(?:\.\d|\b)))/g,"\$1"+separator);
r=(rr[1]?b+ decpoint +rr[1]:b);
if (!isNaN(parseFloat(r))) {
return format_string.replace('#', r);
} else {
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment