0

I have a simple issue that I might just not be thinking through. I am trying to add together several input fields (which are all formatted numbers like 2.00, 3.00, etc.).

But using the following script:

 var sum = parseFloat(0).toFixed(2);
    //using an iterator find and sum the values of checked checkboxes
    $(".amountCheckbox:checked").each(function() {
        var input = parseFloat($(this).closest('tr').find('.amountInput').val());
        console.log(input.toFixed(2));
      sum += input.toFixed(2);
      console.log(sum);
    });
    return sum;

It returns as 02.003.00 rather than 5.00. I would appreciate any help. Thanks!

2
  • 4
    toFixed() returns a string, so you're just doing string concatenation Commented Sep 3, 2020 at 20:44
  • Remove all .toFixed() from your code and just add it once with return like return sum.toFixed(), it will solve your problem. Commented Sep 4, 2020 at 4:24

4 Answers 4

1

As already indicated Number.toFixed() returns a string so + concatenates rather than add the numbers. Give the Array#reduce() method a try:

return $(".amountCheckbox:checked").closest('tr').find('.amountInput')
.map((index,input) => +input.value).get()
.reduce((acc, cur) => acc + cur, 0);

DEMO

$(':checkbox').on('change', function() {
    const sum = $(".amountCheckbox:checked")
    .closest('tr').find('.amountInput')
    .map((index,input) => +input.value).get()
    .reduce((acc, cur) => acc + cur, 0);
    
    console.log( sum.toFixed(2) );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>Row 1</td>
      <td><input type="text" class="amountInput" value="2.00"></td>
      <td><input type="checkbox" class="amountCheckbox"></td>
    </tr>
    <tr>
      <td>Row 2</td>
      <td><input type="text" class="amountInput" value="5.00"></td>
      <td><input type="checkbox" class="amountCheckbox"></td>
    </tr>
    <tr>
      <td>Row 3</td>
      <td><input type="text" class="amountInput" value="1.00"></td>
      <td><input type="checkbox" class="amountCheckbox"></td>
    </tr>
    <tr>
      <td>Row 4</td>
      <td><input type="text" class="amountInput" value="2.20"></td>
      <td><input type="checkbox" class="amountCheckbox"></td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

0

ToFixed will convert the value to a string, so you are just concatenating.

I think what you want to do is sum up all the values before calling toFixed and call toFixed after to format the value as a fixed precision value.

 var sum = 0;

    //using an iterator find and sum the values of checked checkboxes
    $(".amountCheckbox:checked").each(function() {
        var input = parseFloat($(this).closest('tr').find('.amountInput').val());
        console.log(input.toFixed(2));
      sum += input;
      console.log(sum);
    });
    return sum.toFixed(2);

Comments

0

Maybe something like this?

$('table').on('change input','input',ev=>{
 $('#res').text($(".amountCheckbox:checked").get().reduce((a,c)=>
   +$(c).closest('tr').find('.amountInput').val()+a, 0).toFixed(2))
});    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr><td><input type="checkbox" class="amountCheckbox"></td><td>A</td>
<td><input value="1.2345" class="amountInput"></td></tr>
<tr><td><input type="checkbox" class="amountCheckbox"></td><td>B</td>
<td><input value="13.114" class="amountInput"></td></tr>
<tr><td><input type="checkbox" class="amountCheckbox"></td><td>C</td>
<td><input value="3.6345" class="amountInput"></td></tr>
</table>
<br>total: <span id="res"></span>

The summation happens in the .reduce() function and is done over all table rows where the checkbox is checked. Each input value is converted to float by applying the unary operator +. The summation result is then rounded and converted to a string again in .toString(2).

Comments

0
var sum = 0;
//using an iterator find and sum the values of checked checkboxes
$(".amountCheckbox:checked").each(function() {
  var input = parseFloat($(this).closest('tr').find('.amountInput').val());
  console.log(input.toFixed(2));
  sum += input;
  console.log(sum);
});
return sum.toFixed(2);

toFixed() would return string not numeric value.

1 Comment

As var sum = parseFloat(0).toFixed(2); at first line so sum will be string, so sum += input will be string concatenation & it will return incorrect result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.