-1

I am need to add to add the .toFixed(2) value into this jQuery below but not sure where to place it.

right now it displays something like this when the calculation is done:

You are $10.020000000000003 away from Free Shipping!

jQuery(document).ready(function(){
if(jQuery("div.dynamicMiniCart dd.subtotal").length>0) {
var subtotal = jQuery("div.dynamicMiniCart dd.subtotal")[0].textContent;
var subTotalArr = subtotal.split("$");
if(subTotalArr.length>1){
var spendMore = 45 - Number(subTotalArr[1]);
if(spendMore >0)
    jQuery("#show_total").text("You are $"+spendMore+" away from Free Shipping!");
  else
    jQuery("#show_total").text("Made in the USA!");
}
}
})
;
  • 1
    Have you tried putting it anywhere? – Jason P Sep 09 '13 at 18:42
  • 2
    add it to the text itself "You are $"+spendMore.toFixed(2)+" away from Free Shipping!" – Raghu Sep 09 '13 at 18:44
  • You can do that but the more correct way to handle currency is to convert everything to cents, do your operations and then convert back to dollars. – Cymen Sep 09 '13 at 18:48

2 Answers2

0

Here's one option. This'll round it out before your if statement, to make sure your if condition doesn't eval to true just due to a rounding error.

var spendMore = 45 - Number(subTotalArr[1]).toFixed(2);
andi
  • 6,442
  • 1
  • 18
  • 43
  • I tried doing "var spendMore = 45 - Number(subTotalArr[1]).toFixed(2);" but it still displayed too many digits as before. And I got this to work, thanks! "+spendMore.toFixed(2)+" – user2762388 Sep 09 '13 at 18:53
  • I see that, but for me it actually HIDES the entire DIV that gets pulled for this. I've been cleaning code slowly and noticed some older JS libraries and other piecemeal parts. – user2762388 Sep 09 '13 at 19:07
0

http://jsfiddle.net/3rgMX/

jQuery(function(){
    var txt = jQuery("div.dynamicMiniCart dd.subtotal").text(),
        spendLimit=45,
        num = Number(txt.slice(1));
    if(num < spendLimit){
        jQuery("#show_total").text("You are $"+(spendLimit - num).toFixed(2)+" away from Free Shipping!");
    }else{
        jQuery("#show_total").text("Made in the USA!");
    }
});
Daniel
  • 579
  • 2
  • 7