Forum Moderators: coopster

Message Too Old, No Replies

turning javascript into php

         

adamnichols45

9:31 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cant for the life of me remember how to only print the first 4 digits.

my script is as follows

<script language="JavaScript">
<!--
function showpay() {
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));

}

</script>

the answer comes out like 45.5434543534 but I onyl want to display 45.54 can somebody please help or turn it into php code any help would be great! thanks

Little_G

10:00 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

I think this is right:

[pre]function showpay(){
$princ = intval($_GET['loan']);
$term = intval($_GET['months']);
$intr = (intval($_GET['rate']) / 1200);
return princ * intr / (1 - ( pow( 1 / ( 1 + intr ), term ) ) );
// rounded:
// return round( princ * intr / (1 - ( pow( 1 / ( 1 + intr ), term ) ) ), 2 );
}[/pre]

Andrew

[edited by: Little_G at 10:08 pm (utc) on Oct. 27, 2007]

adamnichols45

10:07 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is not working I get the error message the GET is undefined.

Little_G

10:14 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

The code I posted is the conversion to PHP, so your using it server-side, right.

Andrew

Little_G

10:20 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

Just realized I made a mistake.

[pre]<?php
function showpay(){
$princ = intval($_GET['loan']);
$term = intval($_GET['months']);
$intr = (intval($_GET['rate']) / 1200);
return $princ * $intr / (1 - ( pow( 1 / ( 1 + $intr ), $term ) ) );
// rounded:
// return round( $princ * $intr / (1 - ( pow( 1 / ( 1 + $intr ), $term ) ) ), 2 );
}
?>[/pre]

Andrew

[edited by: Little_G at 10:20 pm (utc) on Oct. 27, 2007]

adamnichols45

10:26 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Still getting an error cant make sense of it.

If this is any easier im trying to do it like this.

on page 1 I have text box with number 43.342423

on page 2 how can I just display 43.34

Little_G

10:27 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

Are you sure that you want to convert to PHP? Or do you just want to round in JavaScript?

Andrew

adamnichols45

10:30 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What ever is easier. Im not sure how to do the rounding down have not done it for years.

Little_G

10:31 pm on Oct 27, 2007 (gmt 0)

10+ Year Member



Hi,

OK, take a look at [javascriptkit.com ].

Andrew

adamnichols45

11:03 pm on Oct 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi I got my code to work now.

<script language="JavaScript">
<!--
function showpay() {
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
var original = princ * intr / (1 - (Math.pow( 1 / (1 + intr), term) ) );
var result=Math.round(original*100)/100 //returns 28.45
document.calc.payy.value = Math.round(original*100)/100 //returns 28.45

}

</script>

Only thing is I now want to print it on the page without using input type="text" but it wont work correctly any suggestions?

Little_G

11:16 am on Oct 28, 2007 (gmt 0)

10+ Year Member



Hi,

Something like this:

[pre]
<script language="JavaScript" type="text/javascript">
<!--
function showpay() {
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
var original = princ * intr / (1 - (Math.pow( 1 / (1 + intr), term) ) );
var result = Math.round(original*100)/100 //returns 28.45
document.calc.payy.value = result;
document.getElementById('[b]result[/b]').innerHTML = result;
-->
}
</script>
...
<body>
<p id="[b]result[/b]"></p>
</body>[/pre]

Andrew

adamnichols45

3:52 pm on Oct 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks I got it done in the end! phew now I can sleep.

Cheers.