Jump to content

Recommended Posts

There is this script that calculates a loan by dividing and raising taxes according to amount of installments.

But it is calculating wrong, if you divide 140 for 3, it will work just fine, but if you do it with 999, it wont give the correct result:

 

 

<script>
function calcula(parcela){
var valor= parseFloat(document.getElementById("avista").value);
var parcela= parseFloat(parcela);

document.getElementById("resultado").value = (valor+50)*parcela;

}
</script>

<font face="Verdana" size="2">
Digite aqui o valor do empréstimo:<br>




<!--  Formata dados -->
<script type="text/javascript"><!-- Original:  Mario Costa (mariocosta@openlink.com.br) -->
<!-- Bug fix:   John Kiernan  -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function currencyFormat(fld, milSep, decSep, e) {

	var isIE8 = false;
	if (window.ActiveXObject) { // IE
		// http://www.javascriptkit.com/javatutors/navigator.shtml
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion>=
			 {
			 	isIE8 = true;
			 }
			 else if (ieversion>=7) 
			 {
			 }
			 else if (ieversion>=6)
			 {
			 }
			 else if (ieversion>=5)
			 {
			 }
		}
	}

	var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if (isIE8 /*|| isChrome*/)
{
}
else
{
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	if (whichCode ==  return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}
}
//  End -->
</script>
<!-- Fim da formatação de dados -->





<input type="text" id="avista"> <!-- onkeypress="return(currencyFormat(this,'',',',event))"> -->

<br>
<select name="parcelas" size="1" onchange="calcula(this.value)">
<option value="">Parcelas</option>
<option value="0.3128">2x com juros de % ao mês.</option>
<option value="0.2528">3x</option>
<option value="0.1575">4x</option>
<option value="0.1102">5x</option>
<option value="0.1039">6x</option>
<option value="0.1039">7x</option>
<option value="0.1039">8x</option>
<option value="0.1039">9x</option>
<option value="0.1039">10x</option>
<option value="0.1039">11x</option>
<option value="0.1039">12x</option>
</select>
<br>
<hr size="1" color="gray">
Resultado:
<input type="text" id="resultado" readonly>



</BODY>
</HTML>

 

 

 

Ps: I think it's a problem with

document.getElementById("resultado").value = (valor+50)*parcela;

 

the 50 part

Link to comment
https://forums.phpfreaks.com/topic/256215-calc-going-wrong-with-loan-script/
Share on other sites

CBA to read all that but

function calcula(parcela){
   var valor= parseFloat(document.getElementById("avista").value);
   var parcela= parseFloat(parcela);
   document.getElementById("resultado").value = (valor+50)*parcela;
}

 

function calcula(parcela){
   var valor= parseFloat(document.getElementById("avista").value);
   parcela  = parseFloat(parcela);
   document.getElementById("resultado").value = (valor+50)*parcela;
}

 

 

parcela is a parameter, the var is probably messing it up...

Parcela means installment in english, the taxes are multiplied by avista (one time price) and divided by installments I guess.  :wtf:

 

:confused:

 

what I know is that it calcs 14 per 3 correctly, 140 per 3 correctly, but something like 999 / 3 won't calc correctly...

 

:confused:  :shrug:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.