0

i coded price * Qty function with jquery but not responding daily prices. They response decimal numbers.

Here is code;

 var qty = "3";
 var price = "1.20";
 var res = (qty*price);
 $("#boxres").html(res);

They result 3.5999999999999996

i want to only 3.6 how can i fix it ?

3 Answers3

2

Store things in pennies:

var qty = 3;
var cost = 120;
var res = cost * qty;
var res_str = '£'+Math.floor(res / 100)+'.'+(res %100).toFixed(2);
Eric
  • 95,302
  • 53
  • 242
  • 374
1

try

$("#boxres").html(res.toFixed(1));

http://jsfiddle.net/dfz0bj3z/

Chris Rosete
  • 1,240
  • 15
  • 13
0

Use Math.round

var res = (Math.round((qty*price)*10)/10).toFixed(2);

Edit to force to the 100th. Thanks dave.

TyMayn
  • 1,936
  • 2
  • 18
  • 23