1

I'm new to javascript, and really did try to find a solution but I don't know how to apply it to my specific code.

I have two range fields on a form which I want to use as a loan calculator. So, value1 is for the 'loan amount', value2 is for the 'repayment period'. So value1 needs to be divided by value2, and then it needs to output it into an ID called #fullvalue so that I can show the repayment installment value on the form. My code below is working great, however, I can't for the life of me get it right to output the #fullvalue amount to two decimal places. So that it shows like $79.50 instead of something like $75.56789555

$(window).on('load', function() {
$("form :input").change(function() {
var value1 = $('form #value1').val();
var value2 = $('form #value2').val();
var fullvalue = parseInt(value1, 10) / parseInt(value2, 10);
$('form #fullvalue').val(fullvalue);
});
});
Shtarley
  • 313
  • 9
  • 22

3 Answers3

3

You should use parseFloat as you're wanting partial numbers and use the .toFixed method to get the result you're wanting:

var value1 = $('form #value1').val(); //Value1 from form
var value2 = $('form #value2').val(); //Value2 from form
var fullvalue = parseInt(value1, 10) / parseInt(value2, 10); //Divide one from the other
fullvalue = parseFloat(fullValue).toFixed(2); //Round the result and assign back to fullvalue variable (75.57)
$('form #fullvalue').val(fullvalue); //write the rounded value into the form

That should round for you.

Kyle Muir
  • 3,875
  • 2
  • 22
  • 27
  • Thank you, I read about the toFixed earlier, but didn't know how to apply it properly to my code. I'm still a little confused though, because it still needs to be stored in an id $('form #fullvalue').val(fullvalue); So how would I do that with the last line you added? Is it rounded of first before it's stored, or is it done in the same line... or how does it work? I'm very new to javascript so it confuses me a bit... – Shtarley Jan 24 '16 at 23:56
  • Commented added inline with my answer - does that clear it up? – Kyle Muir Jan 25 '16 at 01:02
0

The best solution involves using the toFixed method. You'll want to use the parseFloat method for this.

parseFloat(number).toFixed(2)

To convert to 2 decimals simply call toFixed(2)

You can also create a function that does this yourself.

function roundToDecimals(number,decimals){
    return Math.round(number * Math.pow(10,decimals))/Math.pow(10,decimals);
}

Then call the function

roundToDecimals(42.545345,2)
=> 42.54
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • Thank you, but I'm really new to javascript so got no clue how to do that in my own code. I coded the above code in my post using tutorials so I don't really know what I'm doing (but trying). – Shtarley Jan 25 '16 at 00:00
0

You can use toFixed like this

$(window).on('load', function() {
$("form :input").change(function() {
var value1 = $('form #value1').val();
var value2 = $('form #value2').val();
var fullvalue =        parseInt(value1.toFixed(2), 10) / parseInt(value2.toFixed(2), 10);
$('form #fullvalue').val(fullvalue);
});
});
Castro Alhdo
  • 293
  • 3
  • 8
  • This won't work quite as expected. E.g. value1 = 75.25, value2 = 89.23. `parseInt(value1.toFixed(2), 10) / parseInt(value2.toFixed(2), 10) //0.8426966...` – Kyle Muir Jan 24 '16 at 23:49
  • I don't think that will work... I don't know much about javascript, but you added the toFixed to the individual values, instead of the value after it's calculated.... so I don't think the end result is going to be acurate. – Shtarley Jan 24 '16 at 23:59