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

miken32
  • 42,008
  • 16
  • 111
  • 154
Jump_Ace
  • 193
  • 1
  • 3
  • 11
  • 1
    Use `number.toLocaleString()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – John Ellmore Nov 08 '17 at 21:48
  • what do you mean by "add a comma" ? You want a decimal number or just add a comma after the number ? – ValLeNain Nov 08 '17 at 21:49
  • regex to add commas to a large number in the first answer here https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – IrkenInvader Nov 08 '17 at 21:51
  • @JohnEllmore Ellmore, how would I implement that? I'm rather new to making these kinds of changes. – Jump_Ace Nov 08 '17 at 21:57
  • The answer by @kemotoe implements this correctly – John Ellmore Nov 08 '17 at 21:59

2 Answers2

2

As stated in the comments use toLocaleString()

Simple example

let number = 123456789;
let numberWithComma = number.toLocaleString();
console.log(numberWithComma);
kemotoe
  • 1,730
  • 13
  • 27
  • I got it put together. Thanks guys! MileagePointsObj.innerHTML = (parseInt(((RaceLength*60*60)/LapTime)*10 / 10, 10) * 10).toLocaleString() – Jump_Ace Nov 08 '17 at 22:47
0

Thank you for the help guys!

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

Jump_Ace
  • 193
  • 1
  • 3
  • 11