Display Commas in Large Numbers: JavaScript

Posted by user3723918 on Stack Overflow See other posts from Stack Overflow or by user3723918
Published on 2014-08-22T03:36:58Z Indexed on 2014/08/22 4:20 UTC
Read the original article Hit count: 114

Filed under:

I'm working on a customized calculator, which is working pretty well except that I can't figure out how to get the generated numbers to display commas within the number. For example, it might spit out "450000" when I need it to say "450,000". This thread gives a number of suggestions on how to create a new function to deal with the problem, but I'm rather new to JavaScript and I don't really know how to make such a function interact with what I have now. I'd really appreciate any help as to how to get generated numbers with commas! :)

HTML:

    <table id="inputValues">
        <tr>
        <td>Percentage:</td>
        <td><input id="sempPer" type="text"></td>
        </tr>

        <tr>
        <td>Price:</td>
        <td><input id="unitPrice" type="text"></td>
        </tr>

        <tr>
        <td colspan="2"><input id="button" type="submit" value="Calculate"></td>
        </tr>
    </table>

    <table id="revenue" class="TFtable">
    <tr>
    <td class="bold">Market Share</td>
    <td class="bold">Partner A</td>
    <td class="bold">Partner B</td>
    </tr>

    <tr>
    <td class="bold">1%</td>
    <td><span id="moss1"></span></td>
    <td><span id="semp1"></span></td>
    </tr>

    </table>

    </form>

JavaScript:

<script>
    function calc() {
        var z = Number(document.getElementById('sempPer').value);
        var x = Number(document.getElementById('unitPrice').value);
        var y = z / 100;
        var dm1 = .01 * 50000 * x * (1-y);
        var se1 = .01 * 50000 * x * y;


            document.getElementById("moss1").innerHTML= "$"+Number(dm1).toFixed(2);
            document.getElementById("semp1").innerHTML= "$"+Number(se1).toFixed(2);
    }
</script>

© Stack Overflow or respective owner

Related posts about JavaScript