-2

I have a script like this, for shipping options

<script language="javascript">
$(document).ready(function()
{   
    var total = 0;
    function calcTotal()
    {
        $("input:checked").each(function()
        {
            //This happens for each checked input field
            var value = $(this).attr("value");
            total += parseInt(value);           

        });
    }
    //This happens when the page loads
    calcTotal();    
    $("#total").append('<input style="border:0px" type="text" name="total" value=" '+ total +' " readonly />');
    $("input:radio").click(function()
    {
        total = 0;
        calcTotal();
        $("#total").html ('<input style="border:0px" type="text" name="total" value=" '+ total +' " readonly />');
    });
});
</script>

using the radio inputs to the option results in total

option 1 <input name="shipp" type="radio" value="1234567" checked />

option 2 <input name="shipp" type="radio" value="2345678" />

output: <div id=total></div>

I need help, how to make the view the total number of formats for thousands?, for example 1234567, the result will be 1,234,567 ?

thanks for help

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
Haidarpesebe
  • 31
  • 1
  • 1
  • 5

2 Answers2

2

Duplicate of:

Add comma to numbers every three digits

From Doug Neiner's answer on thread:

@Paul Creasey had the simplest solution as the regex, but here it is as a simple jQuery plugin:

$.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); }) }

You could then use it like this:

$("span.numbers").digits();

Please Up his answer.

Your Solution would look like: ...

    $("input:checked").each(function()
    {
        //This happens for each checked input field
        var value = $(this).attr("value");
        total += parseInt(value.replace(",",""));           

    });

    //This happens when the page loads
    calcTotal();   
    var totalAsString = total.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");  
    $("#total").append('<input style="border:0px" type="text" name="total" value=" '+ totalAsString +' " readonly />');
    $("input:radio").click(function()
    {
        total = 0;
        calcTotal();
        $("#total").html ('<input style="border:0px" type="text" name="total" value=" '+ totalAsString +' " readonly />');
    });
Community
  • 1
  • 1
Totero
  • 2,524
  • 20
  • 34
0

intVariable.toLocaleString() does everything for you

Mahdi Khalili
  • 1,025
  • 15
  • 32