javascript - How do I format a number to 2 decimal places, but only if there are already decimals? -


i have jquery 1.5+ script, , select quantity in drop-down menu (1,2,3, etc) , multiplies quantity $1.50 show total price. - it's multiplying quantity selected (1, 2, 3, etc) base price of $1.50 - - can't figure out how display price correctly decimals - example: if select quantity of 2, price displays correctly $3 (no decimals). but, if choose 1, or 3, price displays $1.5 / $4.5 - missing 0 in hundredths decimal place.

here's code - idea how show second 0 in case there not 2 decimals? $3 should stay $3, $4.5 should become $4.50, etc - can't work without showing numbers 2 decimals, , that's i'm stuck!

<script type='text/javascript'>          $(function() {                  $('#myquantity').change(function() {                          var x = $(this).val();                                   $('#myamount').text('$'+(x*1.5));// part isn't displaying decimals correctly!         });          });  </script> 

i'm experimenting result = num.tofixed(2); can't work yet.

thank kindly!

working example: http://jsfiddle.net/peeter/jxpzh/

$(document).ready(function() { $('#itemquantityselect_3').change(function() {      var itemprice = 1.50;     var itemquantity = $(this).val();     var quantityprice = (itemprice * itemquantity);     if(math.round(quantityprice) !== quantityprice) {         quantityprice = quantityprice.tofixed(2);     }      $(this).next("span").html("$" + quantityprice);  }); 

});

<form action="/" method="post"> <input type="submit" name="submit" class="button" id="" value="button" /> <select id='itemquantityselect_3' name="itemquantity_3">     <option value='1'>1 item</option>     <option value='2'>2 items</option>     <option value='3'>3 items</option> </select> <span>$1.50</span> 


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -