0

The below solution works great for a simple live calculation however, I am trying to make one small change. I want to round the answer always to 2 decimal places. I have tried adding .toFixed(2) to various points but it doesn't seem to work. Like this,

<script type="text/javascript">
$(document).ready(function() {
$("#mytextfield").on('keyup',function(){
   // alert('pressed')
   var totalcost= $("#totaldays").val() * $(this).val() 
   $(".total_cost").text(totalcost).toFixed(2);
})
});
</script>

Can anyone tell me how I can achieve two decimal places?

Thanks

user1098178
  • 697
  • 3
  • 9
  • 25

3 Answers3

0

Use parseFloat(yourstring) to convert string to number and try this code :

$(document).ready(function() {
    $("#mytextfield").on('keyup',function(){
        var totalcost = parseFloat($("#totaldays").val()) * parseFloat($(this).val())
        $(".total_cost").text(totalcost.toFixed(2));
    })
});
Lucas Willems
  • 6,673
  • 4
  • 28
  • 45
0

Use divided by 100 at the end of it the syntax is like this:

/ 100

Also you need Math.floor of Math.round

0

try this

$(".total_cost").text(totalcost.toFixed(2));

roshan
  • 160
  • 1
  • 7