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

This should do the job:

var formattedNumber = (x * 1.5).toFixed(2).replace(/[.,]00$/, "");

I suggest:

Math.round(floatNumber*100)/100;

It automatically adds 0, 1 or 2 decimal places.


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);

    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/" method="post">
    <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>
</form>