Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Round to the Decimal

    Can someone help me to round the result for txt_price. Also if the decimal can be placed to spots over.

    Try selecting Arizona, that is a good example.

    Thank you


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <form>
    State
    : <br>
    <select name="combo0" id="combo_0" style="width:200px;">
    <option value="">-select-</option>
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="70">Arizona</option>
    <option value="4">Arkansas</option>
    <option value="66">California</option>
    <option value="6">Colorado</option>
    <option value="7">Connecticut</option>
    <option value="8">Delaware</option>
    <option value="9">District of Columbia</option>
    <option value="10">Florida</option>
    <option value="11">Georgia</option>
    <option value="12">Hawaii</option>
    <option value="63">Idaho</option>
    <option value="14">Illinois</option>
    <option value="15">Indiana</option>
    <option value="16">Iowa</option>
    <option value="17">Kansas</option>
    <option value="18">Kentucky</option>
    <option value="19">Louisiana</option>
    <option value="20">Maine</option>
    <option value="21">Maryland</option>
    <option value="22">Massachusetts</option>
    <option value="23">Michigan</option>
    <option value="24">Minnesota</option>
    <option value="25">Mississippi</option>
    <option value="26">Missouri</option>
    <option value="62">Montana</option>
    <option value="28">Nebraska</option>
    <option value="65">Nevada</option>
    <option value="30">New Hampshire</option>
    <option value="31">New Jersey</option>
    <option value="32">New Mexico</option>
    <option value="33">New York</option>
    <option value="34">North Carolina</option>
    <option value="35">North Dakota</option>
    <option value="36">Ohio</option>
    <option value="37">Oklahoma</option>
    <option value="61">Oregon</option>
    <option value="39">Pennsylvania</option>
    <option value="40">Rhode Island</option>
    <option value="41">South Carolina</option>
    <option value="42">South Dakota</option>
    <option value="43">Tennessee</option>
    <option value="44">Texas</option>
    <option value="64">Utah</option>
    <option value="46">Vermont</option>
    <option value="47">Virginia</option>
    <option value="60">Washington</option>
    <option value="49">West Virginia</option>
    <option value="50">Wisconsin</option>
    <option value="51">Wyoming</option>
    </select>
    <BR>
    <br>
    Amount: <br>
    <select name="combo1" id="combo_1" style="width:200px;">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    </select>
    <br>
    <!-- To present an outcome to this field -->
    <br>
    Sub-total: <br>
    <input type="text" name="txtPrice" id="txt_price" readonly="readonly" style="width:200px;">
    </form>

    <script type="text/javascript">

    // avoid using inline script like onclick and onchange, bad practice.

    window.onload = function() {

    var dropChange = function() {

    var v1 = document.getElementById("combo_1").value, v2 = document.getElementById("combo_0").value;

    //document.getElementById("txt_price").value = v1 * v2;

    if (v2 == 70) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 * 1.0795 + 9.50;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 * 1.0795 + 9.80;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 * 1.0795 + 11.50;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 * 1.0795 + 12.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 * 1.0795 + 12.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 * 1.0795 + 14.50;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 * 1.0795 + 14.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 * 1.0795 + 14.50;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 * 1.0795 + 14.50;
    }
    }

    else if (v2 == 60) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 61) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 62) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 63) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 64) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 65) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 == 66) {
    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50 + 8.90;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80 + 10.25;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50 + 15.00;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00 + 17.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00 + 25.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50 + 27.00;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50 + 42.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50 + 43.00;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50 + 43.50;
    }
    }

    else if (v2 != (60 || 61 || 62 || 63 || 64 || 65 || 66 || 70)) {

    if (v1 == 1) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.50;
    }
    else if (v1 == 2) {
    document.getElementById("txt_price").value = v1 * 24.95 + 9.80;
    }
    else if (v1 == 3) {
    document.getElementById("txt_price").value = v1 * 22.95 + 11.50;
    }
    else if (v1 == 4) {
    document.getElementById("txt_price").value = v1 * 20.95 + 12.00;
    }
    else if (v1 == 5) {
    document.getElementById("txt_price").value = v1 * 19.95 + 12.00;
    }
    else if (v1 == 6) {
    document.getElementById("txt_price").value = v1 * 18.95 + 14.50;
    }
    else if (v1 == 7) {
    document.getElementById("txt_price").value = v1 * 17.95 + 14.50;
    }
    else if (v1 == 8) {
    document.getElementById("txt_price").value = v1 * 16.95 + 14.50;
    }
    else if (v1 == 9) {
    document.getElementById("txt_price").value = v1 * 15.95 + 14.50;
    }
    }
    };


    document.getElementById("combo_0").onchange = dropChange;

    document.getElementById("combo_1").onchange = dropChange;

    };
    </script>

    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Holy Overcode, Batman!

    Let's PLEASE rewrite that!

    Code:
    window.onload = function() {
    
       var dropChange = function() {
           var v1 = parseInt( document.getElementById("combo_1").value );
           var v2 = parseInt( document.getElementById("combo_0").value );
    
    // this should be indented, but too bad...
    var price;
    switch ( v2 )
    {
        case 70: 
             var taxplus = 1.0795;
             switch ( v1 )
             {
                case 1: /* fall thru...same as case 2 */
                case 2: price = v1 * 24.95 * taxplus + 9.50; break;
                case 3: price = v1 * 22.95 * taxplus + 11.50; break;
                case 4: price = v1 * 20.95 * taxplus + 12.00; break;
                case 5: price = v1 * 19.95 * taxplus + 12.00; break;
                case 6: price = v1 * 18.95 * taxplus + 14.50; break;
                case 7: price = v1 * 17.95 * taxplus + 14.50; break;
                case 8: price = v1 * 16.95 * taxplus + 14.50; break;
                case 9: price = v1 * 15.95 * taxplus + 14.50; break;
                default: alert("Invalid v1 value for v2=70: " + v1); 
             }
             break;
    
        case 60: case 61: case 62: case 63: case 64: case 65: case 66:
             switch ( v1 )
             {
                case 1: price = v1 * 24.95 + 9.50 + 9.80; break;
                case 2: price = v1 * 24.95 + 9.80 + 10.25; break;
                case 3: price = v1 * 22.95 + 11.50 + 15.00; break;
                case 4: price = v1 * 20.95 + 12.00 + 17.00; break;
                case 5: price = v1 * 19.95 + 12.00 + 25.00; break;
                case 6: price = v1 * 18.95 + 14.50 + 27.00; break;
                case 7: price = v1 * 17.95 + 14.50 + 42.50; break;
                case 8: price = v1 * 16.95 + 14.50 + 43.00; break;
                case 9: price = v1 * 15.95 + 14.50 + 43.50; break;
                default: alert("Invalid v1 value for v2=" + v2 + ": " + v1); 
             }
             break;
    
        default: /* all other v2 values get here: */
             switch ( v1 )
             {
                case 1: price = v1 * 24.95 + 9.50; break;
                case 2: price = v1 * 24.95 + 9.80; break; 
                case 3: price = v1 * 22.95 + 11.50; break;
                case 4: price = v1 * 20.95 + 12.00; break;
                case 5: price = v1 * 19.95 + 12.00; break;
                case 6: price = v1 * 18.95 + 14.50; break;
                case 7: price = v1 * 17.95 + 14.50; break;
                case 8: price = v1 * 16.95 + 14.50; break;
                case 9: price = v1 * 15.95 + 14.50; break;
                default: alert("Invalid v1 value for v2=" + v2 + ": " + v1); 
             }
    }
    document.getElementById("txt_price").value = "$" + price.toFixed(2);
    
    document.getElementById("combo_0").onchange = dropChange;
    documentt.getElementById("combo_1").onchange = dropChange;
    };
    So far as I could see, all of your v1-based prices were the same for all the v2 values from 60 to 66, so I wrote it like that. If some are different, you can split the cases, as need be.

    I highlighted a couple of other omissions or things you asked about.

    Could make this even more compact with use of object arrays, but ehhhh...this is probably okay.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •