I'm building a financing calculator and all of the numbers being outputted to the DOM are in the thousands.
I currently am using .toLocaleString() on one number in my code, and it works (the main productPrice number). I used .toLocatelString() when outputting to the DOM.
However, I can't seem to figure out why when using the same way, it doesn't work on the other numbers. Specifically, the Down Payment, Total and Per Month numbers.
Here's the JS code (the code I've entered .toLocaleString() is at the very bottom):
"use strict";
// Define product price / tax
const productPrice = 105000;
const tax = 0.13;
// Append product price to DOM
const productPriceID = document.getElementById("product-price");
productPriceID.innerHTML = productPrice.toLocaleString();
// Grab the id's of the main product price, down payment, total, per month and button for DOM appending
const downPaymentValue = document.getElementById("down-payment-value");
const totalValue = document.getElementById("total-value");
const perMonthValue = document.getElementById("per-month-value");
const calculateBtn = document.getElementById("calculate");
///////// Calculations
calculateBtn.addEventListener("click", calculate);
function calculate() {
// Grab the value of the month selected
const monthSelected = document.querySelector('input[name="month"]:checked')
.value;
// Grab the value of the down payment percentage selected
const percentageSelected = document.querySelector(
'input[name="percent"]:checked'
).value;
// Calculate down payment percentage based on main price
const totalDownPayment = (productPrice * percentageSelected).toFixed(2);
// Calculate the total
const totalPrice = (productPrice - totalDownPayment).toFixed(2);
// Calculate the per month
const perMonth = (totalPrice / monthSelected).toFixed(2);
// Append down payment to DOM
downPaymentValue.innerHTML =
"<sup>$</sup>" + totalDownPayment.toLocaleString();
downPaymentValue.parentNode.appendChild(downPaymentValue);
// Append total to DOM
totalValue.innerHTML = "<sup>$</sup>" + totalPrice.toLocaleString();
totalValue.parentNode.appendChild(totalValue);
// Append per month to DOM
perMonthValue.innerHTML = "<sup>$</sup>" + perMonth.toLocaleString();
perMonthValue.parentNode.appendChild(perMonthValue);
}
Any idea? Thanks in advance.
perMonth.toLocaleString();with(+perMonth).toLocaleString();