It is necessary to calculate the payment amount according to the formula: (Loan amount * interest rate * number of months + loan amount)

I use this code

$("#summa").text(Math.round($("#hidden").val() * $("#hidden2").val() * q + $("#hidden").val())); 

but the + operator doesn’t matter, it doesn’t add a value, and it adds a number to the resulting number. those. works like a 'number' + 'second number'

  • 2
    You add lines, here you have a problem, try parseInt or parseFloat - Orange_shadow
  • 2
    Operator + does not mow, mows ignorance of the data type. - Dmitry Gvozd

2 answers 2

The fact is that you inserted not numbers, but lines (i.e. text fields). You need to convert all strings to numbers using Number

 $(function() { var q = 4; $("#summa").text(Math.round(Number($("#hidden").val()) * Number($("#hidden2").val()) * q + Number($("#hidden").val()))); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="summa"></div> <input id="hidden" value="2"> <input id="hidden2" value="3"> 

  • Thanks, helped - Danil Shuvaev
  • @ DanilShuvaev, then mark the answer as correct - Yuri
  • I apologize, I just started using this service, I didn’t know that it was necessary, I confirmed it ... I must click on the check mark, right? - Danil Shuvaev
  • @ DanilShuvaev, yes. Well, this is a formality. This is done so that users who have similar problems know what has helped you. Well, the author +15 reputation is charged for this formality) - Yuri

Wrap each operation to get the value in brackets and multiply by 1 so that your string becomes a number.

 ($("#hidden").val() * 1) 
  • Or, alternatively, in theory, it will work +$("#hidden").val() . - Surfin Bird