PDA

View Full Version : Display value entered but round up to calculate question



TYOC
02-21-2012, 06:04 PM
I would really appreciate some assistance with this calculator. The purpose is to determine the premium for two insurance plans when sold together. This is not a homework assignment and is to be used for noncommercial purposes.

I canít figure out the best way to solve the following issues:

The value entered for Aplan Amount should display in text box Aplan limit, but in order for the calculation to work properly, the value entered for Aplan Amount needs to be rounded up to nearest thousand. The same applies for Bplan Amount.

For Example Ė If Aplan Amount entered is $1,189,233.00 and Bplan Amount entered is $986,503.00, the calculations for Aplan Amount and Bplan Amount should be based on $1,190,000.00 and $987,000.00 respectively, but Aplan limit should display $1,189,233.00 and Bplan limit should display $986,503.00. Given these values, the calculator should return $813.50 (instead of $812.76) for Aplan Premium and $2,049.00 (instead of $2,048.01) for Bplan Premium.

Thanks in advance for your assistance.

[CODE]
<html>
<head>
<form name="calc" action="#">
Aplan Amount<input name="aplan"/><br />
Bplan Amount<input name="bplan"/><br />
<input onclick="calculateValues()" value="Calculate" type="button" name="Calculate" /><input type="Reset" value="Clear Fields"> <br />
Aplan Premium<input name="aplanPremium" /><br />
Aplan limit <input name="aplan0" /><br />
Bplan Premium<input name="bplanPremium" /> <br />
Bplan limit <input name="bplan0" /><br />
</form>
<script type="text/javascript">
function resetInputs() {}

function RoundUpToThousand(passParm) {
var resultThou = Math.floor(((+passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function roundCent(passParm) {
var resultCent = Math.floor(((+passParm) + 0.005) * 100) * 0.01;
return resultCent;
}
function formatDecimal(passParm, passPrecision) {
var j1;
var strMtx = [];
var resultDecimal;
var strMid;
var strFin;
passParm = passParm * 1;
strMid = "" + Math.round(eval(passParm) * Math.pow(10, passPrecision));
while (strMid.length <= passPrecision) {
strMid = "0" + strMid;
}
var decPos = strMid.length - passPrecision;
strFin = strMid.substring(0, decPos);
j1 = Math.ceil(strFin.length / 3) - 1;
while (strFin.length > 0) {
strMtx[j1] = strFin.substring(strFin.length - 3, strFin.length);
strFin = strFin.substring(0, strFin.length - 3);
j1--;
}
resultDecimal = strMtx.join(",") + "." + strMid.substring(decPos, strMid.length);
return resultDecimal;
}
function formatDollar(passParm) {
var decPlaces = 2;
var formattedVal = "$" + formatDecimal(passParm, decPlaces);
return formattedVal;
}
function calc1Value(InsuredValue, aB1, aB2, aB3, aB4, aB5, aB6, aB7, pR1, pR2, pR3, pR4, pR5, pR6, pR7) {
var premRate = 0;
var cumPrem = 0;
var loanThou = RoundUpToThousand(InsuredValue) / 1000;
var remainThou = loanThou;
var breakThou = RoundUpToThousand(aB1) / 1000;
if (remainThou > breakThou) {
premRate = Number(pR1);
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB2 - aB1) / 1000;
premRate = Number(pR2);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB3 - aB2) / 1000;
premRate = Number(pR3);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB4 - aB3) / 1000;
premRate = Number(pR4);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB5 - aB4) / 1000;
premRate = Number(pR5);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB6 - aB5) / 1000;
premRate = Number(pR6);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
premRate = Number(pR7);
cumPrem = cumPrem + premRate * remainThou + 48;
}
else {
premRate = Number(pR6);
cumPrem = 4277 + (((InsuredValue - 1000000) / 1000) * premRate) + 48;
}
}
else {
premRate = Number(pR5);
cumPrem = 2075 + (((InsuredValue - 1000000) / 1000) * premRate);
}
}
else {
premRate = Number(pR4);
cumPrem = 275 + (((InsuredValue - 100000) / 1000) * premRate);
}
}
else {
premRate = Number(pR3);
cumPrem = 150 + ((InsuredValue - 50000) / 1000) * premRate;
}
}
else {
premRate = Number(pR2);
cumPrem = premRate * loanThou;
}
}
else {
premRate = Number(pR1);
cumPrem = 50 + cumPrem + premRate * remainThou;
}
cumPrem = roundCent(cumPrem);
result = cumPrem;
return result;
}
function calc2Value(InsuredValue, aOB1, aOB2, aOB3, aOB4, aOB5, aOB6, aOB7, pOR1, pOR2, pOR3, pOR4, pOR5, pOR6, pOR7) {
var premRate = 0;
var cumPrem1 = 0;
var purchThou = RoundUpToThousand(InsuredValue) / 1000;
var remainThou = purchThou;
var breakThou = RoundUpToThousand(aOB1) / 1000;
if (remainThou > breakThou) {
premRate = Number(pOR1);
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB2 - aOB1) / 1000;
premRate = Number(pOR2);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB3 - aOB2) / 1000;
premRate = Number(pOR3);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB4 - aOB3) / 1000;
premRate = Number(pOR4);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB5 - aOB4) / 1000;
premRate = Number(pOR5);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB6 - aOB5) / 1000;
premRate = Number(pOR6);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
premRate = Number(pOR7);
cumPrem1 = 48+ cumPrem1 + premRate * remainThou;
}
else {
premRate = Number(pOR6);
cumPrem1 = 18900 + premRate * remainThou;
}
}
else {
premRate = Number(pOR5);
cumPrem1 = 11400 + (((InsuredValue - 5000000) / 1000) * premRate);
}
}
else {
premRate = Number(pOR4);
cumPrem1 = 375 + (((InsuredValue - 100000) / 1000) * premRate);
}
}
else {
premRate = Number(pOR3);
cumPrem1 = 200 + premRate * remainThou;
}
}
else {
premRate = Number(pOR2);
cumPrem1 = premRate * purchThou;
}
}
else {
premRate = Number(pOR1);
cumPrem1 = 50 + cumPrem1 + premRate * remainThou;
}
cumPrem1 = roundCent(cumPrem1);
result = cumPrem1;
return result;
}
function conditionInput(passParm) {
if (passParm === "") {
result = 0;
}
else {
if (passParm.indexOf("$") > -1) {
passParm = passParm.substring(1);
}
while (passParm.indexOf(",") > -1) {
tempStr = passParm;
passParm = tempStr.substring(0, tempStr.indexOf(",")) + tempStr.substring(tempStr.indexOf(",") + 1);
}
result = passParm;
}
return result;
}
function calculateValues() {
var aplan = conditionInput(document.calc.aplan.value);
var bplan = conditionInput(document.calc.bplan.value);
var aplan0 = aplan;
var bplan0 = bplan;
amtBreak1 = 16000;
amtBreak2 = 50000;
amtBreak3 = 100000;
amtBreak4 = 1000000;
amtBreak5 = 10000000;
amtBreak6 = 15000000;
amtBreak7 = 15000000.01;
premRate1 = 0;
premRate2 = 3;
premRate3 = 2.50;
premRate4 = 2;
premRate5 = 1.50;
premRate6 = 1.25;
premRate7 = 1;
amtOBreak1 = 12000;
amtOBreak2 = 50000;
amtOBreak3 = 100000;
amtOBreak4 = 5000000;
amtOBreak5 = 10000000;
amtOBreak6 = 15000000;
amtOBreak7 = 15000000.01;
premORate1 = 0;
premORate2 = 4;
premORate3 = 3.50;
premORate4 = 2.25;
premORate5 = 1.50;
premORate6 = 1.25;
premORate7 = 1;
if (isNaN(aplan)) {
alert("Invalid amount.");
document.calc.aplan.value = "";
document.calc.aplan.focus();
return (false);
}
if (aplan === 0) {
alert("Please enter a value for Aplan!");
document.calc.aplan.focus();
return false;
}
if (isNaN(bplan)) {
alert("Invalid Bplan.");
document.calc.bplan.value = "";
document.calc.bplan.focus();
return (false);
}
bplanPremium = calc1Value(bplan, amtBreak1, amtBreak2, amtBreak3, amtBreak4, amtBreak5, amtBreak6, amtBreak7, premRate1, premRate2, premRate3, premRate4, premRate5, premRate6, premRate7);
bplanPremium = bplanPremium;
bplanPremium = roundCent(bplanPremium);
aplanPremium = calc2Value(aplan, amtOBreak1, amtOBreak2, amtOBreak3, amtOBreak4, amtOBreak5, amtOBreak6, amtOBreak7, premORate1, premORate2, premORate3, premORate4, premORate5, premORate6, premORate7) - bplanPremium + 35;
document.calc.aplan.value = formatDollar(aplan);
document.calc.bplan.value = formatDollar(bplan);
document.calc.bplanPremium.value = formatDollar(bplanPremium);
document.calc.aplanPremium.value = formatDollar(aplanPremium);
document.calc.aplan0.value = formatDollar(aplan0);
document.calc.bplan0.value = formatDollar(bplan0);
return (true);
}
</script>
</body>
</html>
[CODE]

Philip M
02-21-2012, 06:48 PM
Please note that the ending code tag is [/CODE]

Help us to help you by rectifying this.

TYOC
02-21-2012, 07:46 PM
My apologies. Thanks for pointing that out. Here it is.



<html>
<head>
<form name="calc" action="#">
Aplan Amount<input name="aplan"/><br />
Bplan Amount<input name="bplan"/><br />
<input onclick="calculateValues()" value="Calculate" type="button" name="Calculate" /><input type="Reset" value="Clear Fields"> <br />
Aplan Premium<input name="aplanPremium" /><br />
Aplan limit <input name="aplan0" /><br />
Bplan Premium<input name="bplanPremium" /> <br />
Bplan limit <input name="bplan0" /><br />
</form>
<script type="text/javascript">
function resetInputs() {}

function RoundUpToThousand(passParm) {
var resultThou = Math.floor(((+passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function roundCent(passParm) {
var resultCent = Math.floor(((+passParm) + 0.005) * 100) * 0.01;
return resultCent;
}
function formatDecimal(passParm, passPrecision) {
var j1;
var strMtx = [];
var resultDecimal;
var strMid;
var strFin;
passParm = passParm * 1;
strMid = "" + Math.round(eval(passParm) * Math.pow(10, passPrecision));
while (strMid.length <= passPrecision) {
strMid = "0" + strMid;
}
var decPos = strMid.length - passPrecision;
strFin = strMid.substring(0, decPos);
j1 = Math.ceil(strFin.length / 3) - 1;
while (strFin.length > 0) {
strMtx[j1] = strFin.substring(strFin.length - 3, strFin.length);
strFin = strFin.substring(0, strFin.length - 3);
j1--;
}
resultDecimal = strMtx.join(",") + "." + strMid.substring(decPos, strMid.length);
return resultDecimal;
}
function formatDollar(passParm) {
var decPlaces = 2;
var formattedVal = "$" + formatDecimal(passParm, decPlaces);
return formattedVal;
}
function calc1Value(InsuredValue, aB1, aB2, aB3, aB4, aB5, aB6, aB7, pR1, pR2, pR3, pR4, pR5, pR6, pR7) {
var premRate = 0;
var cumPrem = 0;
var loanThou = RoundUpToThousand(InsuredValue) / 1000;
var remainThou = loanThou;
var breakThou = RoundUpToThousand(aB1) / 1000;
if (remainThou > breakThou) {
premRate = Number(pR1);
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB2 - aB1) / 1000;
premRate = Number(pR2);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB3 - aB2) / 1000;
premRate = Number(pR3);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB4 - aB3) / 1000;
premRate = Number(pR4);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB5 - aB4) / 1000;
premRate = Number(pR5);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB6 - aB5) / 1000;
premRate = Number(pR6);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
premRate = Number(pR7);
cumPrem = cumPrem + premRate * remainThou + 48;
}
else {
premRate = Number(pR6);
cumPrem = 4277 + (((InsuredValue - 1000000) / 1000) * premRate) + 48;
}
}
else {
premRate = Number(pR5);
cumPrem = 2075 + (((InsuredValue - 1000000) / 1000) * premRate);
}
}
else {
premRate = Number(pR4);
cumPrem = 275 + (((InsuredValue - 100000) / 1000) * premRate);
}
}
else {
premRate = Number(pR3);
cumPrem = 150 + ((InsuredValue - 50000) / 1000) * premRate;
}
}
else {
premRate = Number(pR2);
cumPrem = premRate * loanThou;
}
}
else {
premRate = Number(pR1);
cumPrem = 50 + cumPrem + premRate * remainThou;
}
cumPrem = roundCent(cumPrem);
result = cumPrem;
return result;
}
function calc2Value(InsuredValue, aOB1, aOB2, aOB3, aOB4, aOB5, aOB6, aOB7, pOR1, pOR2, pOR3, pOR4, pOR5, pOR6, pOR7) {
var premRate = 0;
var cumPrem1 = 0;
var purchThou = RoundUpToThousand(InsuredValue) / 1000;
var remainThou = purchThou;
var breakThou = RoundUpToThousand(aOB1) / 1000;
if (remainThou > breakThou) {
premRate = Number(pOR1);
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB2 - aOB1) / 1000;
premRate = Number(pOR2);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB3 - aOB2) / 1000;
premRate = Number(pOR3);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB4 - aOB3) / 1000;
premRate = Number(pOR4);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB5 - aOB4) / 1000;
premRate = Number(pOR5);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aOB6 - aOB5) / 1000;
premRate = Number(pOR6);
if (remainThou > breakThou) {
cumPrem1 = cumPrem1 + premRate * breakThou;
remainThou = remainThou - breakThou;
premRate = Number(pOR7);
cumPrem1 = 48+ cumPrem1 + premRate * remainThou;
}
else {
premRate = Number(pOR6);
cumPrem1 = 18900 + premRate * remainThou;
}
}
else {
premRate = Number(pOR5);
cumPrem1 = 11400 + (((InsuredValue - 5000000) / 1000) * premRate);
}
}
else {
premRate = Number(pOR4);
cumPrem1 = 375 + (((InsuredValue - 100000) / 1000) * premRate);
}
}
else {
premRate = Number(pOR3);
cumPrem1 = 200 + premRate * remainThou;
}
}
else {
premRate = Number(pOR2);
cumPrem1 = premRate * purchThou;
}
}
else {
premRate = Number(pOR1);
cumPrem1 = 50 + cumPrem1 + premRate * remainThou;
}
cumPrem1 = roundCent(cumPrem1);
result = cumPrem1;
return result;
}
function conditionInput(passParm) {
if (passParm === "") {
result = 0;
}
else {
if (passParm.indexOf("$") > -1) {
passParm = passParm.substring(1);
}
while (passParm.indexOf(",") > -1) {
tempStr = passParm;
passParm = tempStr.substring(0, tempStr.indexOf(",")) + tempStr.substring(tempStr.indexOf(",") + 1);
}
result = passParm;
}
return result;
}
function calculateValues() {
var aplan = conditionInput(document.calc.aplan.value);
var bplan = conditionInput(document.calc.bplan.value);
var aplan0 = aplan;
var bplan0 = bplan;
amtBreak1 = 16000;
amtBreak2 = 50000;
amtBreak3 = 100000;
amtBreak4 = 1000000;
amtBreak5 = 10000000;
amtBreak6 = 15000000;
amtBreak7 = 15000000.01;
premRate1 = 0;
premRate2 = 3;
premRate3 = 2.50;
premRate4 = 2;
premRate5 = 1.50;
premRate6 = 1.25;
premRate7 = 1;
amtOBreak1 = 12000;
amtOBreak2 = 50000;
amtOBreak3 = 100000;
amtOBreak4 = 5000000;
amtOBreak5 = 10000000;
amtOBreak6 = 15000000;
amtOBreak7 = 15000000.01;
premORate1 = 0;
premORate2 = 4;
premORate3 = 3.50;
premORate4 = 2.25;
premORate5 = 1.50;
premORate6 = 1.25;
premORate7 = 1;
if (isNaN(aplan)) {
alert("Invalid amount.");
document.calc.aplan.value = "";
document.calc.aplan.focus();
return (false);
}
if (aplan === 0) {
alert("Please enter a value for Aplan!");
document.calc.aplan.focus();
return false;
}
if (isNaN(bplan)) {
alert("Invalid Bplan.");
document.calc.bplan.value = "";
document.calc.bplan.focus();
return (false);
}
bplanPremium = calc1Value(bplan, amtBreak1, amtBreak2, amtBreak3, amtBreak4, amtBreak5, amtBreak6, amtBreak7, premRate1, premRate2, premRate3, premRate4, premRate5, premRate6, premRate7);
bplanPremium = bplanPremium;
bplanPremium = roundCent(bplanPremium);
aplanPremium = calc2Value(aplan, amtOBreak1, amtOBreak2, amtOBreak3, amtOBreak4, amtOBreak5, amtOBreak6, amtOBreak7, premORate1, premORate2, premORate3, premORate4, premORate5, premORate6, premORate7) - bplanPremium + 35;
document.calc.aplan.value = formatDollar(aplan);
document.calc.bplan.value = formatDollar(bplan);
document.calc.bplanPremium.value = formatDollar(bplanPremium);
document.calc.aplanPremium.value = formatDollar(aplanPremium);
document.calc.aplan0.value = formatDollar(aplan0);
document.calc.bplan0.value = formatDollar(bplan0);
return (true);
}
</script>
</body>
</html>

jmrker
02-22-2012, 12:13 AM
Consider using this function in your calculations instead of your current one.


<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
var aplan = 1189233.00
var bplan = 986503.00
function RoundUp1000(V) {
return Math.floor((V / 1000)+1)*1000;
}
</script>

</head>
<body>
<button onclick="alert(aplan+' --> '+RoundUp1000(aplan)+'\n'+bplan+' --> '+RoundUp1000(bplan))">
Round up by 1000</button>

</body>
</html>

TYOC
02-22-2012, 04:47 PM
Thank you so much for your reply jmrker. You certainly provided an answer to the question posed in the title of my post. However, it doesnít solve my problem. In hindsight, I should have been more specific with my request.
The amounts for Aplan and Bplan are not fixed. I just pulled those numbers out of a hat. The example given was meant to define the actual premiums for those amounts and to compare them with what the script currently returns for premiums.
The Aplan and Bplan amounts entered need to be displayed (as entered) in the Aplan and Bplan limit text boxes. Defining every possible amount would be, to say the least, a nightmarish task. To calculate the premiums due, the amounts entered for Aplan and Bplan need to be rounded up to the nearest thousand and passed to function calc1Value and function calc2Value, which is what I thought would be accomplished by my existing code here:


function RoundUpToThousand(passParm) {
var resultThou = Math.floor(((+passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}



I believe the code you suggest performs the same function. (Please correct me if Iím wrong Ė Iím a newbie and in no way want to sound argumentative). What I really need to know why my code is not getting the job doneÖ
Thanks again for your knowledge and your time. I really appreciate it.

Philip M
02-22-2012, 04:57 PM
jmrker gave you the code but you need to incorporate it in your own script. His figures for aplan and bplan were simply illustrative.

Aplan Amount<input name="aplan"/><br />
Bplan Amount<input name="bplan"/><br />

so


var apln = document.calc.aplan.value; // the value entered by the user
var bpln = document.calc bplan.value;
Roundup1000(apln); // round up to next 1000
// do whatever is required with the rounded-up value
RoundUp1000(bpln); // repeat for the Bplan


I have to say that your code is terribly convoluted and could be much simplified. There are errors as well - return (false) should be return false

jmrker
02-22-2012, 06:53 PM
Thank you so much for your reply jmrker. You certainly provided an answer to the question posed in the title of my post. However, it doesnít solve my problem. In hindsight, I should have been more specific with my request.
The amounts for Aplan and Bplan are not fixed. I just pulled those numbers out of a hat. The example given was meant to define the actual premiums for those amounts and to compare them with what the script currently returns for premiums.
The Aplan and Bplan amounts entered need to be displayed (as entered) in the Aplan and Bplan limit text boxes. Defining every possible amount would be, to say the least, a nightmarish task. To calculate the premiums due, the amounts entered for Aplan and Bplan need to be rounded up to the nearest thousand and passed to function calc1Value and function calc2Value, which is what I thought would be accomplished by my existing code here:


function RoundUpToThousand(passParm) {
var resultThou = Math.floor(((+passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}



I believe the code you suggest performs the same function. (Please correct me if Iím wrong Ė Iím a newbie and in no way want to sound argumentative). What I really need to know why my code is not getting the job doneÖ
Thanks again for your knowledge and your time. I really appreciate it.

I guess I still don't quite understand the problem.
This bit of code show that BOTH functions produce the same result.
Mine may be a bit smaller, but no significant difference in the return value.

The value returned can be placed into a different variable for continued calculations.
Is it your formula that you are having difficulty with?
Can you specifically point out where you code does not do what you want with defined variables?


<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?p=1196246#post1196246

function RoundUp1000(V) { return Math.floor((V / 1000)+1)*1000; }

function RoundUpToThousand(passParm) {
var resultThou = Math.floor(((+passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function $_(IDS) { return document.getElementById(IDS); }

function displayResults() {
var aplan = $_('aplan').value;
var bplan = $_('bplan').value;

var str = '1st function<br>';
str += aplan+' --> '+RoundUpToThousand(aplan)+'<br>'
+bplan+' --> '+RoundUpToThousand(bplan);
str += '<p>2nd function<br>';
str += aplan+' --> '+RoundUp1000(aplan)+'<br>'
+bplan+' --> '+RoundUp1000(bplan);
return str;
}
</script>
</head>
<body>
aplan: <input type="text" id="aplan" value="1189233.00"><br>
bplan: <input type="text" id="bplan" value="986503.00"><br>

<button onclick="document.getElementById('showCalcs').innerHTML = displayResults()">
Round UP TO nearest 1000</button>
<div id="showCalcs" style="width:300px; height 100px; border:1px solid red">
</div>
</body>
</html>

TYOC
02-23-2012, 01:03 AM
jmrker
Again the problem is not you Ė itís me and my nondescript explanation. Maybe I should explain how the premiums are calculated.

For the Aplan, thereís a $50.00 minimum charge. The premium is $50.00 up to and including $12,000, $4.00 per thousand or fraction over $12,000 up to and including $50,000, $3.50 per thousand or fraction for the next $50,000, $2.25 per thousand or fraction over $100,000 up to and including $5,000,000, $1.50 per thousand or fraction over $5,000,000 up to and including $10,000,000, $1.25 per thousand or fraction over $10,000,000 up to and including $15,000,000 and $1.00 per thousand or fraction over $15,000,000.

For the Bplan, thereís a $50.00 minimum charge. The premium is $50.00 up to and including $16,000, $3.00 per thousand or fraction over $16,000 up to and including $50,000, $2.50 per thousand or fraction for the next $50,000, $2.00 per thousand or fraction over $100,000 up to and including $1,000,000, $1.50 per thousand or fraction over $1,000,000 up to and including $10,000,000, $1.25 per thousand or fraction over $10,000,000 up to and including $15,000,000, $1.00 per thousand or fraction over $15,000,000.

When both plans are issued together, the premium for Bplan is the premium for Bplan and the premium for Aplan is the premium for Aplan less the premium for Bplan + $35.00.

For example, if Aplan is $158,459.00 and Bplan is $157,459.00 then the premiums are to be calculated as follows:

Aplan
$50 (up to 12K) + $150 (up to 50K) + $175 (up to 100K) + $132.75 (for the 59K over 100K) = $507.75

Bplan
$50 (up to 16K) + $100 (up to 50K) + $125 (up to 100K) + $116 (for the 58K over 100K) = $391.00

When issued together
Bplan premium = $391.00
Aplan premium = $507.75 Ė $391.00 + $35 = $151.75

Given this information, can you tell me where Iíve failed and why the script is not accurately computing the premiums?

Philip M is correct to say my code is terribly convoluted, but itís the only way I know to do it. I am trying to learnÖ

Philip M
02-23-2012, 08:26 AM
jmrker - may I respectfully point out that your RoundUP1000() function should be


function RoundUp1000(V) { return Math.ceil((V / 1000))*1000; }
as otherwise it will round 23000 to 24000.

Here is how I would do it (example for plan A only, planB is obviously similar, so repeat with appropriate adjustments to the numbers. The rest is trivial).


<script type = "text/javascript">

function RoundUp1000(V) { return Math.ceil((V / 1000))*1000; }

function calc() {

var a = Number(document.myform.aplan.value) || 0; // trap NaN entries
a = RoundUp1000(a);
alert (a); // for testing

var band1 = 0; var band2 = 0; var band3 = 0; var band4 = 0; var band5 = 0; var band6 = 0;

if (a > 15000000) {band1 = a - 15000000}
if (a > 10000000) {band2 = a - 10000000 - band1}
if (a > 5000000) {band3 = a - 5000000 - band1 - band2}
if (a > 100000) {band4 = a - 100000 - band1 - band2 - band3}
if (a > 50000) {band5 = a - 50000 - band1 - band2 - band3 - band4}
if (a > 12000) {band6 = a - 12000 - band1 - band2 - band3 - band4 - band5}

var premA = 0;
if (a > 0) {
premA = 50 + (band6/1000 * 4) + (band5/1000 *3.5) + (band4/1000 * 2.25) + (band3/1000 * 1.5) + (band2/1000 * 1.25) + (band1/1000 *1)
}

alert ("$" + premA.toFixed(2));

}


</script>

<form name = "myform">
APlan <input type="text" name="aplan" value= "">
<input type="button" value="Calculate" onClick="calc()">
</form>

I make the premium for amount insured of 158459.00 to be $509.75 (confirmed by manual calculation), not $507.75. You will want to do more checking as I do not get the same figures as your earlier examples. For amount insured of 1189233 I get premium $2829.50.

jmrker
02-23-2012, 02:27 PM
jmrker - may I respectfully point out that your RoundUP1000() function should be


function RoundUp1000(V) { return Math.ceil((V / 1000))*1000; }
as otherwise it will round 23000 to 24000.



Good catch!
:thumbsup:

TYOC
02-23-2012, 11:24 PM
Phillip M

Again you saved the day. I've been working on this for three months and you probably banged that code out in less than three minutes. I can't thank you enough.

No worries about the calculation being off. That was easily fixed by changing 50 to 48 and adding if statements for values under 12K and 16K making a and b = 50.

Thanks again. You rock!