1

I'm creating a product page that requires the price to update when the quantity value is changed. Two form fields are used: .orig_price and #quantity. These values are obviously multiplied together.

I'm trying to split the multiplied value, so that I can print the correct format (27.7043454575 should be 27.70).

My code:

jQuery("#quantity").change(function() {

         jQuery("#pricediv").hide();

// store form values
         var origprice = jQuery(".orig_price").val().substr(1); 
         var qty = jQuery("#quantity").val();

// calculate price
         var sumValue = origprice * qty;

// split price
         var splitprice = sumValue.split("."); 
         var pricepound = splitprice[0];
         var pricepenny = splitprice[1].substring(0,2);   

// update price

         jQuery("#pricediv").html('£' + pricepound + '.' + pricepenny);
         jQuery("#pricediv").fadeIn(1500);
});

If I remove the split and use sumValue everything works (but format is wrong). Does split not work on a calculation?

4
  • In addition to the great answer underneath it might be worth adding that the problem with your approach was that sumValue is a number (because of the multiplication that happened beforehand) so you cannot apply a String method like split. String(sumValue).split("."); would work Commented Mar 27, 2012 at 14:13
  • 1
    @JamesMcLaughlin Why not floats when dealing with currency? How else are you going to compute a percentage tax, for instance, if not with floating-point math? Commented Mar 27, 2012 at 14:13
  • @mblase75 Because floats are imprecise by nature? It's better to work with whole pennies where possible (and it's certainly possible in the OP's code). Commented Mar 27, 2012 at 14:14
  • @JamesMcLaughlin Floats are imprecise, yes, but typically not until the tenth decimal place or thereabouts. I've never heard this argument from anyone else and can't think of a single good example. If you're using division or percentages AT ALL in your arithmetic, floating-point math is a necessity. All you need to do is round off to the correct decimal value at every step in the computation. Commented Mar 27, 2012 at 14:16

2 Answers 2

4

You'll want to use sumValue.toFixed(2)

var sumValue = 27.7043454575;
sumValue.toFixed(2) // 27.70

.split does not exist on numeric types. You would have to use sumValue.toString().split('.'), and either way, this would be more inconvenient than simply sticking to .toFixed

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

Comments

0

You can use toFixed and parseInt() like so:

jQuery("#quantity").change(function() {

         jQuery("#pricediv").hide();

// store form values
         var origprice = parseInt(jQuery(".orig_price").val().substr(1),10); 
         var qty = parseInt(jQuery("#quantity").val(),10);

// calculate price
         var sumValue = origprice * qty;

// split price
         var price = sumValue.toFixed(2);  

// update price

         jQuery("#pricediv").html('£' + price);
         jQuery("#pricediv").fadeIn(1500);
});

toFixed determines the number of points after a decimal, and parseInt type-casts the input to an integer (the 10 is unnecessary but there to show it's decimal base 10), because when getting data from a form field it sometimes comes back as a string and messes up your math.

Comments

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.