originalish | function formatNumber(number, thousands) {
thousands = thousands || ',';
var string = String(Math.max(0, Math.abs(number).toFixed(0))),
length = string.length,
end = /^\d{4,}$/.test(string) ? length % 3 : 0;
return (end ? string.slice(0, end) + thousands : '') +
string.slice(end).replace(/(\d{3})(?=\d)/g, '$1' + thousands);
}
formatNumber(1234578.91);
formatNumber(1234578);
| ready |
tweak1 | function formatNumber(number, thousands) {
thousands = thousands || ',';
var string = String(Math.max(0, Math.abs(number).toFixed(0))),
length = string.length,
end = /^\d\d\d\d$/.test(string) ? length % 3 : 0;
return (end ? string.slice(0, end) + thousands : '') +
string.slice(end).replace(/(\d\d\d)(?=\d)/g, '$1' + thousands);
}
formatNumber(1234578.91);
formatNumber(1234578);
| ready |
tweak2 | function formatNumber(number, thousands) {
thousands = thousands || ',';
var string = String(Math.max(0, Math.abs(number).toFixed(0))),
length = string.length,
end = length > 3 ? length % 3 : 0;
return (end ? string.slice(0, end) + thousands : '') +
string.slice(end).replace(/(\d\d\d)(?=\d)/g, '$1' + thousands);
}
formatNumber(1234578.91);
formatNumber(1234578);
| ready |
tweak3 | function formatNumber(number, thousands, decimal) {
thousands = thousands || ',';
decimal = decimal || '.';
number+='';
var flot = '';
if (RegExp('\\' + decimal).test(number)) {
flot = number.substring(number.lastIndexOf(decimal));
number = number.substring(0, number.length - flot.length)
}
var length = number.length,
end = length > 3 ? length % 3 : 0;
return (end ? number.slice(0, end) + thousands : '') +
number.slice(end).replace(/(\d\d\d)(?=\d)/g, '$1' + thousands) + flot;
}
formatNumber(1234578.91);
formatNumber(1234578);
| ready |