1

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.

1
  • perMonth is a string, not a number. replace perMonth.toLocaleString(); with (+perMonth).toLocaleString(); Commented Jan 27, 2020 at 21:41

1 Answer 1

6

It is because your other numbers are being converted to string via toFixed. So toLocaleString does not do anything.

Do all of your math in numbers and convert to strings at the end.

const totalDownPayment = (productPrice * percentageSelected);
const totalPrice = (productPrice - totalDownPayment);
const perMonth = (totalPrice / monthSelected);

convert to text with options argument to specify number of decimals:

const totalDownPaymentStr = totalDownPayment.toLocaleString(navigator.language, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
const totalPriceStr = totalPrice.toLocaleString(navigator.language, { minimumFractionDigits: 2, maximumFractionDigits: 2 })
// ...

See MDN documentation for more information about the options argument.

Sign up to request clarification or add additional context in comments.

2 Comments

@dandavis There really not that new.
@brandon Thank you very much! Works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.