Вы сказали:
Например в costPrice приходит 53220 при 80000 в pfixsum то в percentfix будет 28376904,00
Как процент percentfix округлить до целого двух знаков ?
const costPrice = extractNumber($('#cost_price').text());
function calculateMarginPrice(cost, marginPercent) {
return Math.floor(cost / (1 - marginPercent / 100));//Math.round
}
// Формула наценки
function calculateMarkupPrice(cost, markupPercent) {
if (markupPercent > 1e12) return Infinity;
return Math.round(cost * (1 + markupPercent / 100));//Math.floor
}
// Обработчик изменения процента
$('#percentfix').on('input', function() {
const percent = parseFloat($(this).val()) || 0;
const customMargin = calculateMarginPrice(costPrice, percent);
$('#pfixsum').val(customMargin);
});
// Обработчик изменения суммы
$('#pfixsum').on('input', function() {
const percent = parseFloat($('#percentfix').val()) || 0;
const customPercent = calculateMarkupPrice(costPrice, percent);
$('#percentfix').val(customPercent.toFixed(2));
});
<table>
<thead>
<tr>
<th rowspan="2">Цена, Юани ¥</th>
<th rowspan="2">Наша себ-сть, ₽</th>
<th colspan="2">Маржа, %</th>
<th colspan="2">Маржа, свой %</th>
</tr>
<tr class="sub-header">
<th>45</th>
<th>25</th>
<th><input type="number" id="percentfix" style=" width: 80px;"> %</th>
</tr>
</thead><tbody>
<tr>
<td>2860 ¥</td>
<td id="cost_price">53220 ₽</td>
<td>96 763 ₽</td>
<td>70 960 ₽</td>
<td><input type="number" id="pfixsum" style=" width: 100px;"> ₽</td>
</tr>
</tbody>
</table>