I have a jQuery slider which the user can drag selecting values from 0-100 (or 1-100).
I want to use the slider value the user selects as a percentage of another variable value.
Its to work out a users pension value and a lump sum value. The idea is that the higher the slider value the greater percentage of the users pension is used as a pension sacrifice which in turn increases their lump sum value.
for example:
fsb = 400000
pension_2008 = fsb/80 - (pensionSacrifice);
slider value = 0 to 100 (set by user)
(maximum value the pensionScarifice can be is a quarter of the pension_2008 value)
then my formula is set as:
pensionSacrifice = parseInt(pension_2008) /4 *(lumpSumSlide)/100;
the answer (if user slides to 100) should = 1250
e.g.
pensionSacrifice 5000/4 = 1250
*100 /100 = 1250
however, my pensionSacrifice textbox returns a value of 1001.25 and pension_2008 = 4005.05
and sometimes I get inconsistent results slightly when I move the slider back and forth.
pensionSacrifice should vary from 0 to 1250 (but goes from 0 to 1001)
pension_2008 should vary from 3750 to 5000 (but goes from around 4000 to 5000)
anyone know what is wrong with my formula please?
maybe I should set the min and max value of slider to a variable value rather 0-100 and working it out as a percentage?
I can submit full code if required, but just trying to keep it simple.
What I have tried:
function doMath() {
var lumpSumSlide = $("#slidervalue").val();
var stdlumpSum = $("#stdlumpSum").val();
var pension_2008 = $("#pension_2008").val();
var annualSalary = $("#annualSalary").val();
var fsb = $("#fsb").val();
var years = $("#years").val();
var pensionSacrifice = $("#pensionSacrifice").val();
var maxPensionSacrifice = $("#pensionSacrifice").val();
fsb = (annualSalary * years);
pension_2008 = fsb/80 - (pensionSacrifice);
stdlumpSum = pension_2008 * 3;
maxPensionSacrifice=pension_2008/4;
pensionSacrifice = maxPensionSacrifice * (lumpSumSlide)/100;
$("#pension_2008").val(custRound(pension_2008,2));
$("#annualSalary").val(custRound(annualSalary,2));
$("#fsb").val(custRound(fsb,2));
$("#stdlumpSum").val(custRound(stdlumpSum,2));
$("#pensionSacrifice").val(custRound(pensionSacrifice,2));
$("#maxPensionSacrifice").val(custRound(maxPensionSacrifice,2));
}
function custRound(x,places) {
return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}