Web Analytics Made Easy -
StatCounter javascript rounding (not decimals) - CodingForum

Announcement

Collapse
No announcement yet.

javascript rounding (not decimals)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • javascript rounding (not decimals)

    I am looking for javascript where a value if it falls between:

    A thru B = Value for A is shown
    C thru D = Value for C is shown

    So lets say I have value= $90,495.00

    these are my options:
    $80,000=$396/yr.
    $100,000=$432/yr.
    $120,000=$480/yr.
    $140,000=$516/yr.
    $160,000=$576/yr.
    $180,000=$636/yr.
    $200,000=$708/yr.

    So then for above stated value, $396 is chosen.

    if value= $159,999.00 then $516 is chosen.

    note:
    (I want to leave my script open to change, so that if in the future if I decide to round up as well then if value is a cent over halfway it will round up to the next value; example being if value= $110,000.01 it will choose $480.)

  • #2
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var Ary=new Array();
    Ary[0]=[80000,'$396/yr'];
    Ary[1]=[100000,'$432/yr'];
    Ary[2]=[120000,'$480/yr'];
    Ary[3]=[140000,'$516/yr'];
    Ary[4]=[160000,'$576/yr'];
    Ary[5]=[180000,'$636/yr'];
    Ary[6]=[200000,'$708/yr'];
    Ary[7]=[20000000000,'Too Much'];
    
    function CkLimits(obj){
     val=obj.value.replace(/\s|,|\$/g,'');
     valop='Not Enough'
     if (Ary[0][1]<val){
      for (i=1;i<Ary.length;i++){
       if (Ary[i][0]>val){
        valop=Ary[i-1][1];
        break
       }
      }
     }
     document.getElementById('Result').value=valop;
    }
    
    //-->
    </script></head>
    
    <body>
    <input size=10 onblur="CkLimits(this);">
    <br>
    <input id="Result" size=10 >
    <br>
    I am looking for javascript where a value if it falls between:
    <br>
    <br>A thru B = Value for A is shown
    <br>C thru D = Value for C is shown
    <br>
    <br>So lets say I have value= $90,495.00
    <br>
    <br>these are my options:
    <br>$80,000=$396/yr.
    <br>$100,000=$432/yr.
    <br>$120,000=$480/yr.
    <br>$140,000=$516/yr.
    <br>$160,000=$576/yr.
    <br>$180,000=$636/yr.
    <br>$200,000=$708/yr.
    <br>
    <br>So then for above stated value, $396 is chosen.
    <br>
    <br>if value= $159,999.00 then $516 is chosen.
    <br>
    <br>note:
    <br>(I want to leave my script open to change,
    <br>so that if in the future if I decide to round up as well
    <br>then if value is a cent over halfway it will round up to the next value;<br>
    example being if value= $110,000.01 it will choose $480.)
    </body>
    
    </html>
    BTW Please is good
    Last edited by vwphillips; Jul 29, 2005, 12:12 PM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

    Comment


    • #3
      thanks..I'm gonna implement it and respond.

      sorry for not adding please, i forgot...if you read ALL my posts you will notice that I am very polite with all my posts and give credit when due. It was my mistake and sorry if I offended anyone.

      thanks.SalJB

      Comment


      • #4
        thanks, sorry it took so long to respond it been very hectic.
        I tried implementing your code but I am having trouble.

        this is what I have done:
        Code:
        <script language="JavaScript" type="text/javascript">
        <!--
        var insuranceLevel=new Array(7);
        insuranceLevel[0]=[80000,'396'];
        insuranceLevel[1]=[100000,'432'];
        insuranceLevel[2]=[120000,'480'];
        insuranceLevel[3]=[140000,'516'];
        insuranceLevel[4]=[160000,'576'];
        insuranceLevel[5]=[180000,'636'];
        insuranceLevel[6]=[200000,'708'];
        
        function CkLimits(inputBox){
         mortgageAmount=inputBox.value.replace(/\s|,|\$/g,'');
         insuranceAmount='Not Enough'
         if (insuranceLevel[0][1]<mortgageAmount){
          for (i=1;i<insuranceLevel.length;i++){
           if (insuranceLevel[i][0]>mortgageAmount){
            insuranceAmount=insuranceLevel[i-1][1];
            break
           }
          }
         }
         document.getElementById('Result').value=insuranceAmount;
        }
        
        //-->
        </script>
        Same as your with my names. If I enter 80000 to 99000 it gives the right response. But anything above 99999 gives me the "not enough" response. Also, its weird some under 80000 will work but some won't. it so random (i noticed if they have multiple 9's or 6's it will).

        Also, i need no response if anything over 200000 is entered.

        Thanks for your help.

        SalJB

        Comment


        • #5
          Code:
          var insuranceLevel = [
            [Number.NEGATIVE_INFINITY, 'Not enough'],
            [80000, '396'],
            [100000, '432'],
            [120000, '480'],
            [140000, '516'],
            [160000, '576'],
            [180000, '636'],
            [200000, '708'],
            [Number.POSITIVE_INFINITY, 'Too high']
          ];
          function checkLimit(inputBox) {
            var i = 1, insuranceAmount, mortgageAmount = new Number(inputBox.value.replace(/\s|,|\$/g,''));
            for(; i < insuranceLevel.length; i++) {
              if(mortgageAmount < insuranceLevel[i][0]) {
                insuranceAmount = insuranceLevel[i - 1][1];
                break;
              }
            }
            document.getElementById('Result').value=insuranceAmount;
          }
          dumpfi
          Last edited by dumpfi; Aug 1, 2005, 07:04 PM.
          "Failure is not an option. It comes bundled with the software."
          ....../)/)..(\__/).(\(\................../)_/)......
          .....(-.-).(='.'=).(-.-)................(o.O)...../<)
          ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
          ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
          Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

          Comment


          • #6
            thanks dumpfi,

            For some reason anything over 200000 i showing up as "708" instead of "too much".

            I can't figure it out.I tried messing around with Number.MAX_VALUE but no luck. Any suggestions. Anything under 80000 will show "not Enough".

            thanks.SalJB

            btw

            I also want to implement it to use it with percentages. For example, something like:

            these are my options:
            Value 1 is .00% thru .05% of Value2 = .00063
            Value 1 is .05% thru .10% of Value2 = .00041
            Value 1 is .10% thru .15% of Value2 = .00025
            Value 1 is .15% thru .20% of Value2 = .00012

            So if Value 1 is .00% thru .05% of Value2, then .00063 is multiplied to value 2 and the answer is value3.

            thanks again.SalJB
            Last edited by saljb; Aug 2, 2005, 12:10 PM.

            Comment


            • #7
              I have tested the code I posted and have NOT found any inconsistance

              what do you mean by no responce

              you can change field[7][1] to what you want or leave field [7] out altogether

              cant see any advantages in dumpfi modifications

              I will not work on the % if you are continuing to experience 'inconsistancies'
              Last edited by vwphillips; Aug 2, 2005, 02:48 PM.
              Vic

              God Loves You and will never love you less.

              http://www.vicsjavascripts.org/Home.htm

              If my post has been useful please donate to http://www.operationsmile.org.uk/

              Comment


              • #8
                I'm not sure, does math.round() come up with decimals or whole numbers? If it is whole numbers it would be a lot easyer to use.
                Single Paradox
                My Site (Under construction)

                Not too bad for a 15 year old

                Comment


                • #9
                  does math.round() come up with decimals or whole numbers?
                  Returns a supplied numeric expression rounded to the nearest integer.

                  but whats that to do with the topic?
                  Vic

                  God Loves You and will never love you less.

                  http://www.vicsjavascripts.org/Home.htm

                  If my post has been useful please donate to http://www.operationsmile.org.uk/

                  Comment


                  • #10
                    Oh I didn't read the whole post, I thought he wanted to round to the nearest whole number so that he won't have decimals. And if that is the case, I should have merely said parseInt();
                    Single Paradox
                    My Site (Under construction)

                    Not too bad for a 15 year old

                    Comment


                    • #11
                      thanks vwphillips,
                      I implemented your code again and it mostly worked. I modified it a bit

                      Code:
                      <script language="JavaScript" type="text/javascript">
                      <!--
                      var Ary=new Array();
                      Ary[0]=[79999,''];
                      Ary[1]=[80000,'$396/yr'];
                      Ary[2]=[100000,'$432/yr'];
                      Ary[3]=[120000,'$480/yr'];
                      Ary[4]=[140000,'$516/yr'];
                      Ary[5]=[160000,'$576/yr'];
                      Ary[6]=[180000,'$636/yr'];
                      Ary[7]=[200000,'$708/yr'];
                      Ary[8]=[200001,''];
                      
                      function CkLimits(obj){
                       val=obj.value.replace(/\s|,|\$/g,'');
                       valop=''
                       if (Ary[0][1]<val){
                        for (i=1;i<Ary.length;i++){
                         if (Ary[i][0]>val){
                          valop=Ary[i-1][1];
                          break
                         }
                        }
                       }
                       document.getElementById('Result').value=valop;
                      }
                      
                      //-->
                      </script>
                      whenever a number was 200000 or over it would show "not enough" and anything under 80000 would show "396" when it should show nothing. Thanks again.

                      Any idea of how I can implement percentages like I explained earlier?

                      I also want to implement it to use it with percentages. For example, something like:

                      these are my options:
                      if Value 1 is .00% thru .05% of Value2 = .00063
                      if Value 1 is .05% thru .10% of Value2 = .00041
                      if Value 1 is .10% thru .15% of Value2 = .00025
                      if Value 1 is .15% thru .20% of Value2 = .00012

                      So if Value 1 is .00% thru .05% of Value2, then .00063 is multiplied to value 2 and the answer is value3.

                      thanks again.SalJB
                      I appreciate everyone's help. Thanks. SalJB

                      Comment

                      Working...
                      X