Solved

How can I round up the users input to the next thousand using javascript?

Posted on 2008-10-31
4
746 Views
Last Modified: 2012-08-14
I am creating a title fee calculator and they need every dollar to round up to the next thousand.
I have used  < input onblur="this.value=1000*Math.round(this.value/1000)"> to round the user's input  to the nearest thousand, but I need it to go to the next thousand.  When I alter it with Math.round(this.value/1000)+1 it works perfectly.  18,200 goes to 19,000, which is desirable.  However, 19,000 will go to 20,000, which is not desirable.  I need the numbers that are already an even thousand to stay an even thousand.  The calculator I'm creating goes to 1,000,000.00, so I need a way for when the user enters an even thousand number such as 220,000 it will remain 220,000.  but if they enter 220,001 it needs to go to 221,000.
I am attaching my form and code.  Is there something I'm missing, or is there an easier way to achieve the correct premium amount?
Any help would be greatly appreciated, I've been working on this WAY too long.
<FORM name="rateform">

      <p align="left"><b><font size=2 face="Verdana" color="#006600"> Enter Policy Amount</font></b> <br />

                  <br />

                  <INPUT name=insurance size="35" style=background-color: #FFFFFF; border: 1px inset #C0C0C0; font-size:11px; onblur="this.value=1000*Math.round(this.value/1000+1)">

</TR>

      <p align="left"><font size=1 face="Verdana">Do not enter any 

                currency symbols or commas<br>

                </FONT>                

      <p align="left"><font color="#006600" size="2" face="Verdana">

                <input onclick=calculateRate(); type=button value="Calculate Base Premium:" name=calculate_rate style="border:1px solid #006600; font-family: Arial; font-size: 8pt; background-color: #81AA71; color:#FFFFFF; font-weight:bold" />

                </font>

      <p align="left"><b> <font size=2 face="Verdana" color="#006600">Base Premium</font></b>

                    </TD>

        <br />

                    <br />

                    <INPUT size=35 value=0 name=Loan_Policy style="border: 1px inset #C0C0C0; font-size:11px"></TD>

                  

                    <script language=JavaScript>
 
 

function calculateRate(){

  var insurancevalue;

  var loan_Policy = "";
 

  insurancevalue = document.rateform.insurance.value;

  loan_Policy = basic_premium(insurancevalue);

 

    document.rateform.Loan_Policy.value = "$" + loan_Policy;
 

}

     

function basic_premium(value){

  var premium;

  var overmin;
 
 
 

    

    

  function  validateNumeric( strValue ) {

  var objRegExp  =  /^[0-9]*$/;
 

  //check for numeric characters

  return objRegExp.test(strValue);

}
 

if( !validateNumeric(value))

	    { premium = alert("Please enter the value in whole dollar ammount without commas or decimals");

      window.location.href = "titlefee-loan2.shtml";

    }   

    

 if( !validateNumeric(value))

	    { premium = 0;

    }   
 
 

	else if ( (value >= 1) & (value <= 16000) )

       {  overmin = ((50)); 

       premium = (overmin);

    }

	else if ( (value >= 16001) & (value <= 17000) )

       {  overmin = ((51)); 

       premium = (overmin);

    }

	else if ( (value >= 17001) & (value <= 49999) )

    {  overmin = ((value)); 

       premium = (overmin*.003)-3;

    }

	else if ( (value >= 50000) & (value <= 50000) )

    {  overmin = ((value)); 

       premium = (overmin*.003)-3;

    }

	else if ( (value >= 50001) & (value <= 99999) )

    {  overmin = ((value)); 

       premium = (overmin*.0025)+25;

    }

	else if ( (value >= 100000) & (value <= 100000) )

    {  overmin = ((value)); 

       premium = (overmin*.0025)+25;

    }

	else if ( (value >= 100001) & (value <= 49999) )

    {  overmin = ((value)); 

       premium = (overmin*.002)+75;

    }

	else if ( (value >= 500000) & (value <= 500000) )

    {  overmin = ((value)); 

       premium = (overmin*.002)+75;

    }

	else if ( (value >= 500001) & (value <= 999999) )

    {  overmin = ((value)); 

       premium = (overmin*.00175)+200;

    }

	else if ( (value >= 10000000) & (value <= 10000000) )

    {  overmin = ((value)); 

       premium = (overmin*.00175)+200;

    }
 
 

	 var temp1 = (premium);

    

    var s1 = temp1.toString();

    var decimalExists = s1.indexOf('.')

     

    if(decimalExists == -2)

        s1 = s1+".00";

        

    else

  {  

    if( (s1.length - decimalExists) == 2) 

      s1 = s1+"0";

      

{

	

	}

	

}

    return s1

}

                    </script>

    </FORM>

Open in new window

0
Comment
Question by:intheory
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:bitter_chicken
Comment Utility
0
 
LVL 5

Accepted Solution

by:
bitter_chicken earned 500 total points
Comment Utility
i.e.

<input onblur="this.value=1000*Math.ceil(this.value/1000)">
0
 

Author Closing Comment

by:intheory
Comment Utility
Thank you!  You're a life saver.  Just signed up tonight as a member, was well worth the money to me!
0
 

Author Comment

by:intheory
Comment Utility
Quick answer!  Perfect results, works beautifully!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now