@Nikitos08

Match.round, где ошибка?

Преобразовал строку в число но все равно получаю NaN , помогите новичку ((

Добрый день! Подскажите как округлить значение переменной, пробовал .toFixed() и Match.round() но скорее всего неправильно их использую
function creditRes($li) {

        var creditGetImage = $li.querySelector(".itemImage").innerHTML.replace(/"/g, "'");
        var creditGetItemTitle = $li.querySelector(".creditTitle").innerHTML;
        var creditGetPrice = Number($li.querySelector(".main-price").innerHTML);
        var creditCartPrice = "Стоимость: " + creditGetPrice + " грн";
        var creditResultShow = "ПОЛОВИНА: ";
        var creditPolovina = "Первый платеж: " + creditGetPrice / 3 + " грн"; 
        var creditPolovinaSecond = "Второй платеж: " + Number(creditGetPrice / 3) + " грн";
        var creditPolovinaThird = "Третий платеж: " + creditGetPrice / 3 + " грн";
        var firstSend = "Первый платеж: " + creditGetPrice / 2 + " грн";
        var secondSend = "Второй платеж: " + Number(creditGetPrice / 2) / 3 + " грн";
        var thirdSend = "Третий платеж: " + (creditGetPrice / 2) / 3 + " грн";
        var fourSend = "Четвертый платеж: " + (creditGetPrice / 2) / 3 + " грн";
        var creditResultShowSecond = "3 ПЛАТЕЖА: ";
    
        document.getElementById('itemTitleShow').innerHTML = creditGetItemTitle;
        document.getElementById('itemImageShow').innerHTML = creditGetImage;
        document.getElementById('itemPriceStatic').innerHTML = creditCartPrice;
        document.getElementById('itemPriceFirst').innerHTML = creditResultShowSecond;
        document.getElementById('itemPriceSecond').innerHTML = creditResultShow;

        document.getElementById('itemfirstSend').innerHTML = firstSend;
        document.getElementById('itemsecondSend').innerHTML = Math.round(secondSend);
        document.getElementById('itemthirdSend').innerHTML = thirdSend;
        document.getElementById('itemfourSend').innerHTML = fourSend;

        document.getElementById('itemPolovina').innerHTML = creditPolovina;
        document.getElementById('itemPolovinaSecond').innerHTML = creditPolovinaSecond;
        document.getElementById('itemPolovinaThird').innerHTML = creditPolovinaThird;

        document.getElementById('creditCart').style.display = "block";

    }
  • Вопрос задан
  • 156 просмотров
Решения вопроса 1
IonDen
@IonDen
JavaScript developer. IonDen.com
Ваша проблема в том, что вы пытаетесь округлить строку.
Нужно вначале сделать округление, а потом уже добавлять всякие тексты, вроде "Второй платеж" и т.п.

Т.е. НЕ(!) так:
var secondSend = "Второй платеж: " + Number(creditGetPrice / 2) / 3 + " грн";
document.getElementById('itemsecondSend').innerHTML = Math.round(secondSend);

А вот так:
var secondNumber = Math.round((creditGetPrice / 2) / 3);
var secondSend = "Второй платеж: " + secondNumber + " грн";
document.getElementById('itemsecondSend').innerHTML = secondSend;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы