Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML
<html>
    <head>
        <title>Page Title</title>
        <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    </head>
       <body>
         <p value="223">JQuery is </p>

<!--!The code prints an integral number like 4! But I'm actually a decimal number 4.4-->

         <p value="4.4">JQuery is </p> 
       <div></div>

       </body>

   
    <script type="text/javascript">
    	
    $( document ).ready(function() {
    var arr=new Array(); $('p').each(function() { 
    $(this).click(function() {
    arr.push($(this).attr("value"));
 
    var total = 0;
    $.each(arr,function(i) {
    total += arr[i] << 0;
     
    $("div").html(total);
          });
       });
    });
 });

    </script>

</html>


What I have tried:

I tried to create ARRAY and try to connect the result of all the values inside
Posted
Updated 11-Mar-17 7:41am
v2
Comments
Bryian Tan 11-Mar-17 13:31pm    
What is the issue/question?
Shabi Levi 11-Mar-17 13:38pm    
The code prints an integral number like 4! But I'm actually a decimal number 4.4
Shabi Levi 11-Mar-17 13:45pm    
@Bryian Tan
All the work you and best short!
Thank you.
Afzaal Ahmad Zeeshan 11-Mar-17 13:39pm    
Never mind
Shabi Levi 11-Mar-17 13:41pm    
Do you understand what you write?
Because I really do not understand you ...
Understanding what my problem anyway? <

1 solution

I don't see why you need a left shift, the parseFloat function should do it.

JavaScript
total += parseFloat(arr[i]);// << 0;

Sample[^]

UPDATE: On the total, you might need to add logic to call toFixed method to control the decimal. I notice 4.4+4.4+4.4 = 13.200000000000001. With .ToFixed(2) the output will end up 13.20

JavaScript
$("div").html(total.toFixed(2));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900