将逗号添加到我的字符串

时间:2017-11-08 21:45:57

标签: javascript comma

我需要帮助在MileagePointsObj.innerHTML函数中向calcMileagePoints添加逗号。现在它被设置为舍入到最接近的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>

提前致谢!

杰罗姆

2 个答案:

答案 0 :(得分:2)

如评论中所述,使用toLocaleString()

简单示例

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

答案 1 :(得分:0)

谢谢你的帮助!

前程万里(MileagePointsObj.innerHTML)=(parseInt(((RaceLength * 60 * 60)/ LapTime)* 10 / 10,10)* 10).toLocaleString()