Question

I'm trying to get an input to round up at two different times in an equation but I keep ending up with decimals. Ideally the equation should take 'footage', multiply it by 12, round up to the nearest whole number, multiply by 3.5, multiply by 1.05%, round up again to the nearest whole number, and put it as the value for 'total' which is used for the value of 'picketQuantity'. Unfortunately I keep getting decimals and answers no where close to what I was expecting. For example if you enter '100' into 'footage', '6ft' into 'fenceHeight' and '1x3.5x6' ideally 'total', and therefore 'picketQuantity', should return 361 but I'm getting 447.3.

If anyone can point out where I went wrong it'd be greatly appreciated. Thanks y'all! Here is a JSFiddle - http://jsfiddle.net/gv0029/xw3DA/

HTML:

<form>
<fieldset id="fence">
    <div name="inputFence" class="inputFence">
        <legend><strong>Fence Description</strong>
        </legend>
        <label>Footage:
            <input name="footage_1" class="footage" />
        </label>
        <select name="fenceHeight_1" class="fenceHeight">
            <option value="select">Select Fence Height</option>
            <option value="6" id="fH6">6 Ft.</option>
            <option value="8" id="fH8">8 Ft.</option>
        </select>
        <select name="fenceStyle_1" class="fenceStyle">
            <option value="select">Style</option>
            <option value="bnb" id="bnb">Board on Board</option>
            <option value="sbs" id="sbs">Side By Side</option>
        </select>
        <select name="picketSize_1" class="picketSize">
            <option value="select">Picket Size</option>
            <option value="1x3.5x6" id="1x4">1 x 3.5 x 6</option>
            <option value="1x4x6" id="1x4">1 x 4 x 6</option>
            <option value="1x5x6" id="1x4">1 x 5 x 6</option>
            <option value="1x5.5x6" id="1x4">1 x 5.5 x 6</option>
            <option value="1x6x6" id="1x4">1 x 6 x 6</option>
        </select>
        <legend><strong>Post Type</strong>
        </legend>
        <label>Picket Quantity
            <input name="picketQuantity_1" class="picketQuantity" />
        </label>
    </div>
</fieldset>

</form>

JS:

//Quantity for Pickets          
$(document.body).on('keypress keydown keyup change', '[class^="footage"],[class^="fenceHeight"], [class^="picketSize"],[class^="fenceStyle"], [class^="picketQuantity"]', function() {
            var parts = $(this).attr('name').split("_");
                fenceNumber = parts[1],

                footage = parseFloat($(":input[name='footage_" + fenceNumber + "'" + ']').val(), 10),
                fenceHeight = $(":input[name='fenceHeight_" + fenceNumber + "'" + ']').find('option:selected').val(),
                fenceStyle = $(":input[name='fenceStyle_" + fenceNumber + "'" + ']').find('option:selected').val(),
                picketSize = $(":input[name='picketSize_" + fenceNumber + "'" + ']').find('option:selected').val(),             
                picketQuantity = $(":input[name='picketQuantity_" + fenceNumber + "'" + ']'),
                total = '';

        if (!isNaN(Number(fenceHeight))) {
                if(fenceHeight == '6') {
                    if (fenceStyle == 'sbs') {
                         if (picketSize == '1x3.5x6' || picketSize == "1x4x6" || picketSize == "1x5x6") {
                            total = Math.ceil((Math.ceil((footage * 12) / 3.5))*1.05);                  
                         } else if (picketSize == '1x5.5x6' || picketSize == "1x6x6") {
                            total = Math.ceil((Math.ceil((footage * 12) / 5.5)) * 1.05);
                         } else {
                            total = "Select Picket Size";
                         }
                            picketQuantity.val(total);
                    } else if (fenceStyle == 'bnb') {
                         if (picketSize == '1x3.5x6' || picketSize == "1x4x6" || picketSize == "1x5x6") {
                            total = ((Math.ceil((footage * 12) / 8.5) * 3) * 1.05);
                         } else if (picketSize == '1x5.5x6' || picketSize == "1x6x6") {
                            total = ((Math.ceil((footage * 12) / 10.5) * 3) * 1.05);
                         } else {
                            total = "Select Picket Size";
                         }
                            picketQuantity.val(total);
                    } else {
                        picketQuantity.val("Select Fence Style");
                    }   
                } else if (fenceHeight == '8') {
                    total = (Math.ceil(footage / 8))*3;
                    picketQuantity.val(total);
                }
            } else {
                picketQuantity.val("Select Fence Height");
            }
}); 
Was it helpful?

Solution

At the second fenceStyle case (when fenceStyle equals bnb) there is no second Math.ceil functions in equations.

Updated fiddle: http://jsfiddle.net/xw3DA/1/.

OTHER TIPS

You are multipling the whole thing with 1.05. Watch out for the braces.

For example, instead of

((Math.ceil((footage * 12) / 8.5) * 3) * 1.05);

you can do something like this:

Math.ceil((Math.ceil((footage * 12) / 8.5) * 3) * 1.05);

But I don't know if it's the correct equation for your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top