2

I'm not familiar with javascript, apologies if this is a simple task.

I am trying to update a total with options selected via a HTML form using checkboxes but I'm having issues with the formatting of the numbers. I have this working, but I'm having issues with the formatting. The current behaviour is that no decimal points are displayed, but functionality is correct. If I change parseInt to parseFloat, I get the decimal points, but it adds many decimal points if I check and uncheck items. If I add two items, that would give me a figure of 5.90 it only displays 5.9 instead of 5.90.

The desired behaviour would be to only have 2 decimal points (I'm not sure why it even displays things like 2.95000001) and when the amount is 5.90 it displays correctly instead of 5.9

Here is my code so far

<head>
<script type="text/javascript">
    var total = 0;
    function test(item){
        if(item.checked){
           total+= parseInt(item.value);
        }else{
           total-= parseInt(item.value);
        }
        //alert(total);
        document.getElementById('Totalcost').innerHTML = total;
    }
</script>
</head>
<body>
<label>
<input type="checkbox" value="2.95" onClick="test(this);"  />Product 1</label>
<label>
<input type="checkbox" value="2.95" onClick="test(this);"  />Product 2</label>
<h3>Total Amount : $<span id="Totalcost"> </span></h3>

Thank you.

EDIT : I also tried changing this line:

total-= parseInt(item.value);

to this

total-= parseInt(item.value).toFixed(2);

But it just adds the amounts to the end of the total instead of adding them up (like this output : Total Amount : £02.002.002.00

2
  • Surely a search for "round to 2 decimal points javascript" would bring up many, many answers. Commented Jun 10, 2017 at 17:38
  • I did, but it hasn't worked for me, hence posting here. Commented Jun 10, 2017 at 17:40

2 Answers 2

3

toFixed(2) i think is what you need

https://www.w3schools.com/jsref/jsref_tofixed.asp

<head>
<script type="text/javascript">
    var total = 0;
    function test(item){
        if(item.checked){
           total+= parseFloat(item.value);
        }else{
           total-= parseFloat(item.value);
        }
        //alert(total);
        document.getElementById('Totalcost').innerHTML = total.toFixed(2);
    }
</script>
</head>
<body>
<label>
<input type="checkbox" value="2.95" onClick="test(this);"  />Product 1</label>
<label>
<input type="checkbox" value="2.95" onClick="test(this);"  />Product 2</label>
<h3>Total Amount : $<span id="Totalcost"> </span></h3>

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

6 Comments

Thanks, I added that in this format total-= parseInt(item.value).toFixed(2); but it then adds a zero before the amount (and after) but more importantly I get this Total Amount : £02.002.002.00 instead of adding the amounts it adds them to the end!
should parseInt be parseFloat ?
I tried changing to to parseFloat again, but when I check multiple items, I get this Total Amount : £02.952.952.95 instead of the amounts being added
That works perfect, thanks. I had put the .toFixed(2) in the wrong place :-( Could I ask one more question? How could I initially display the Total as 0.00 instead of it being blank? Thanks again for your help.
<h3>Total Amount : $<span id="Totalcost">0.00</span></h3> and your welcome
|
1

if you are looking to have only 2 decimal number after the "." you can try this code

 var total = 0;
        function test(item){
            if(item.checked){
               total+= parseFloat(item.value)
            }else{
               total-= ParseFloat(item.value)
            }
            //alert(total);
            document.getElem

entById('Totalcost').innerHTML = total.toFixed(2);
    }

1 Comment

Thank you for your response and help

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.