не надо код, подскажите алгоритм
function shorten(val) {
if (val <= 10000) {
return val.toString();
}
const thousands = val / 1000;
const rounded = Math.round(thousands);
const deviation = Math.sign(thousands - rounded);
return `${[ '≈', '', '>' ][deviation + 1]}${rounded} т.`;
}
function formatNumber(value)
{
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 ");
}
function shortNumber(price)
{
const base = +formatNumber(price).split(' ')[0];
if (+(`${base}000`) < price && price <= +(`${base}500`)) {
return `>${base} т.`;
} else if (price > (+(`${base}000`) + 500)) {
return `≈${base+1} т.`;
} else if (+(`${base}000`) === price) {
return `${base} т.`;
}
}
shortNumber(12000) // 12 т.
shortNumber(12500) // >12 т.
shortNumber(12600) // ≈13 т.
function formatNumber(number) {
if (number <= 1e4) return number.toString();
const rest = (number / 1e3) % 1;
const thousands = Math.floor(number / 1e3) + " т";
if (rest === 0) {
return thousands;
}
if (rest < 0.5) {
return ">" + thousands;
}
if (rest === 0.5) {
return ">" + thousands;
}
if (rest > 0.5) {
return "≈" + thousands;
}
}