sum numbers with js dont work properly "Intl.NumberFormat"

i have page with table of products and for eah procut there is a price, when i change the qty it’s get the total price…

but i have this problem:
when the numbers are lower then "1,000"
the sum is ok

ex:

  1. prod 1: 200
  2. prod 2: 200
  3. prod 3: 500
  4. total: 900

but when the numbers are greater then "1,000" the comma is the problem…
i get wrong answer…

ex:

  1. prod 1: 200
  2. prod 2: 1,200
  3. prod 3: 300
  4. total: 501

the problem is with the comma….
how i can fix this problem?

when i’m removing the "Intl.NumberFormat" from the total of each product i get the right answer but for some cases i get number like this: "1.000031545"
wich i dont need.. i need only the first 2 digit after the dot…

CODE: (simplefied)

<table>
    <tr>
       <td style="text-align: center;">
           <label for="prod[<?php echo $prodid; ?>]"><input class='qty' style="font-size: 12px;" type="text" name="qty_<?php echo $prodid; ?>" placeholder="qty" minlength="1" maxlength="3" size="2"></label>
       </td>
       <td class='price'>
           <?php echo $prodprice; ?>
       </td>
       <td width="40" class="sum">0</td>
    </tr>
    <tr>
       <td colspan="3">
           <span class="output"></span>
       </td>
</table>

<script type="text/javascript">

function getTotal(){
    var total = 0;
    $('.sum').each(function(){
        total += parseFloat(this.innerHTML);
    });

    $('#total').text(total);

            givenNumber = total; 
            nfObject = new Intl.NumberFormat('en-US'); 
            output = nfObject.format(givenNumber); 
            document.querySelector('.output').textContent = total; 
}

getTotal();

$('.qty').keyup(function(){
    var parent = $(this).parents('tr');
    var price = $('.price', parent);
    var sum = $('.sum', parent);
    var value = parseInt(this.value) * parseFloat(price.get(0).innerHTML||0);
            givenNum1 = value; 
            nfObj = new Intl.NumberFormat('en-US'); 
            outpu = nfObj.format(givenNum1); 
    sum.text(outpu);
    getTotal();
})

</script>

189 thoughts on “sum numbers with js dont work properly "Intl.NumberFormat"”

  1. Hello this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if
    you have to manually code with HTML. I’m starting a blog soon but have no coding knowledge so
    I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

    Reply
  2. Hi would you mind sharing which blog platform you’re working with?
    I’m going to start my own blog soon but I’m having a difficult time making
    a decision between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your design seems different then most
    blogs and I’m looking for something unique.
    P.S My apologies for being off-topic but I had to ask!

    Reply
  3. Unquestionably believe that which you said. Your favorite justification seemed to
    be on the net the easiest thing to be aware of.
    I say to you, I definitely get annoyed while people consider worries that they plainly do not know about.

    You managed to hit the nail upon the top and defined out the whole thing without having
    side-effects , people could take a signal.
    Will likely be back to get more. Thanks

    Reply
  4. Greetings from Florida! I’m bored to death at work so I decided
    to check out your site on my iphone during lunch break.

    I really like the info you present here and can’t wait
    to take a look when I get home. I’m surprised at how quick your blog loaded on my
    mobile .. I’m not even using WIFI, just 3G .. Anyways, very good site!
    scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

    Reply
  5. Hello there, I discovered your blog by way of Google whilst looking for a
    similar matter, your web site got here up, it appears to be like good.
    I have bookmarked it in my google bookmarks.
    Hello there, just changed into aware of your weblog via Google, and
    located that it’s truly informative. I’m going to be careful for brussels.
    I’ll appreciate in the event you continue this in future.
    Lots of other folks will probably be benefited out of your writing.
    Cheers! asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr

    Reply
  6. I like the helpful info you supply to your
    articles. I will bookmark your blog and take a look at again here frequently.
    I am slightly sure I will learn lots of new stuff right here!
    Best of luck for the next!

    Reply
  7. An outstanding share! I have just forwarded this onto a colleague who had
    been conducting a little homework on this.
    And he actually ordered me breakfast because I stumbled upon it for him…

    lol. So let me reword this…. Thank YOU for the meal!! But yeah,
    thanx for spending some time to discuss this issue here on your internet site.

    Reply
  8. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get got an edginess over that you
    wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very
    often inside case you shield this hike.

    Reply
  9. Hey there! Someone in my Myspace group shared this website with us so I came to take a look.
    I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers!
    Superb blog and superb style and design.

    Reply
  10. Simply desire to say your article is as surprising.
    The clarity on your post is simply great and that i can assume you are a professional in this
    subject. Well along with your permission let me to snatch your RSS feed to
    stay up to date with imminent post. Thank you a million and please continue the gratifying work.

    Reply
  11. Hmm it appears like your site ate my first comment (it was super long) so I
    guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any helpful hints for first-time blog writers?
    I’d certainly appreciate it.

    Reply

Leave a Comment