function formatAmountInput(value) {
value = value.replace(/[^\d,]/g, '');
const commaIndex = value.indexOf(',');
let integerPart;
let decimalPart = '';
if (commaIndex >= 0) {
integerPart = value.slice(0, commaIndex);
decimalPart = value.slice(commaIndex + 1);
} else {
integerPart = value;
}
// Только цифры
integerPart = integerPart.replace(/\D/g, '');
decimalPart = decimalPart.replace(/\D/g, '');
// Максимум 18 цифр всего
const totalDigits = integerPart + decimalPart;
if (totalDigits.length > 18) {
const allowedDecimalLength = Math.max(
0,
18 - integerPart.length
);
decimalPart = decimalPart.slice(0, allowedDecimalLength);
}
// Пробелы между тысячами
if (integerPart) {
integerPart = Number(integerPart).toLocaleString('ru-RU');
}
return commaIndex >= 0
? `${integerPart},${decimalPart}`
: integerPart;
}