0

I need help adding a comma to MileagePointsObj.innerHTML in calcMileagePoints function. Right now it's set to round to the nearest 10.

<script type="text/javascript">     
        window.onload=function() {
            LapTimeObj = document.getElementById('InputLaptime');
            RaceLengthObj = document.getElementById('InputRaceLength');
            MileagePointsObj = document.getElementById('TDMileagePoints');
            document.getElementById('btnReset').onclick = resetInputs;
            document.getElementById('btnCalc').onclick = calcMileagePoints;
        }
        function resetInputs() {
            LapTimeObj.value = '';
            RaceLengthObj.value = '';
            MileagePointsObj.innerHTML = '';
        }
        function calcMileagePoints() {
            var LapTime = new Number(LapTimeObj.value);
            var RaceLength = new Number(RaceLengthObj.value);
            MileagePointsObj.innerHTML = '';

            MileagePointsObj.innerHTML = parseInt(((RaceLength*60*60)/LapTime)*10 / 10, 10) * 10;
        }
    </script>

Thanks in advance!

Jerome

5
  • 1
    Use number.toLocaleString(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 8, 2017 at 21:48
  • what do you mean by "add a comma" ? You want a decimal number or just add a comma after the number ? Commented Nov 8, 2017 at 21:49
  • regex to add commas to a large number in the first answer here stackoverflow.com/questions/2901102/… Commented Nov 8, 2017 at 21:51
  • @JohnEllmore Ellmore, how would I implement that? I'm rather new to making these kinds of changes. Commented Nov 8, 2017 at 21:57
  • The answer by @kemotoe implements this correctly Commented Nov 8, 2017 at 21:59

2 Answers 2

2

As stated in the comments use toLocaleString()

Simple example

let number = 123456789;
let numberWithComma = number.toLocaleString();
console.log(numberWithComma);

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

1 Comment

I got it put together. Thanks guys! MileagePointsObj.innerHTML = (parseInt(((RaceLength*60*60)/LapTime)*10 / 10, 10) * 10).toLocaleString()
0

Thank you for the help guys!

MileagePointsObj.innerHTML = (parseInt(((RaceLength*60*60)/LapTime)*10 / 10, 10) * 10).toLocaleString()

Comments

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.