Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2008-10-31
Medium Priority
758 Views
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;

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

}

var overmin;

function  validateNumeric( strValue ) {
var objRegExp  =  /^[0-9]*\$/;

//check for numeric characters
return objRegExp.test(strValue);
}

if( !validateNumeric(value))
window.location.href = "titlefee-loan2.shtml";
}

if( !validateNumeric(value))
}

else if ( (value >= 1) & (value <= 16000) )
{  overmin = ((50));
}
else if ( (value >= 16001) & (value <= 17000) )
{  overmin = ((51));
}
else if ( (value >= 17001) & (value <= 49999) )
{  overmin = ((value));
}
else if ( (value >= 50000) & (value <= 50000) )
{  overmin = ((value));
}
else if ( (value >= 50001) & (value <= 99999) )
{  overmin = ((value));
}
else if ( (value >= 100000) & (value <= 100000) )
{  overmin = ((value));
}
else if ( (value >= 100001) & (value <= 49999) )
{  overmin = ((value));
}
else if ( (value >= 500000) & (value <= 500000) )
{  overmin = ((value));
}
else if ( (value >= 500001) & (value <= 999999) )
{  overmin = ((value));
}
else if ( (value >= 10000000) & (value <= 10000000) )
{  overmin = ((value));
}

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>
``````
0
Question by:intheory
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 5

Expert Comment

ID: 22855903
0

LVL 5

Accepted Solution

bitter_chicken earned 2000 total points
ID: 22855905
i.e.

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

Author Closing Comment

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

Author Comment

ID: 22855921
Quick answer!  Perfect results, works beautifully!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
###### Suggested Courses
Course of the Month8 days, 13 hours left to enroll