Jump to content

Recommended Posts

I NEED TO CHANGE THE FOLLOWING JAVASCRIPT CALCULATOR INTO PHP

I'M NOT SURE HOW TO DO THIS, CAN SOMEONE PLEASE HELP ME OUT

THANK YOU IN ADVANCE

 

function calculate(num) {

var tax_on = 0;

var tax_to = 0;

 

num = num.replace(/[^0-9\.]/g,'');

if ( num <= 55000 ) {

tax_on = (num*0.005);

tax_to = (num*0.005);

}

if ( num > 55000 && num <= 250000 ) {

tax_on = (num*0.01)-275;

tax_to = (num*0.01)-275;

}

if ( num > 250000 && num <= 400000 ) {

tax_on = (num*0.015)-1525;

tax_to = (num*0.01)-275;

}

if ( num > 400000 ) {

    tax_on = (num*0.02)-3525;

tax_to = (num*0.02)-4275;

}

    var Sum = tax_on + tax_to;

 

 

document.forms.the_form.tax_on.value = format(tax_on,2);

document.forms.the_form.tax_to.value = format(tax_to,2);

document.forms.the_form.sum.value = format(Sum,2);

 

}

function calculate_com(num) {

var tax_on2 = 0;

var tax_to2 = 0;

 

num = num.replace(/[^0-9\.]/g,'');

if ( num <= 55000 ) {

tax_on2 = (num*0.005);

tax_to2 = (num*0.005);

}

else if ( num > 55000 && num <= 250000 ) {

tax_on2 = (num*0.01)-275;

tax_to2 = (num*0.01)-275;

}

else if ( num > 250000 && num <= 400000 ) {

tax_on2 = (num*0.015)-1525;

tax_to2 = (num*0.01)-275;

}

else if ( num > 400000 && num <= 40000000 ) {

tax_on2 = (num*0.015)-1525;

tax_to2 = (num*0.015)-2275;

}

else {

    tax_on2 = (num*0.015)-1525;

tax_to2 = (num*0.01)+197725;

}

    var Sum2 = tax_on2 + tax_to2;

 

 

document.forms.the_form2.tax_on2.value = format(tax_on2,2);

document.forms.the_form2.tax_to2.value = format(tax_to2,2);

document.forms.the_form2.Sum2.value = format(Sum2,2);

 

}

function format(no, dp)

{

    dp = Math.pow(10, dp);

    var no = (Math.round(no * dp) / dp) + "";

    var first = no.split(".");

    var tmp = new Array;

    var counter = 0;

    var start = first[0].length % 3;

    if (start) tmp[counter++] = first[0].substr(0, start);

    for (var i = start ; i < first[0].length ; i += 3)

        tmp[counter++] = first[0].substr(i, 3);

    first[0] = tmp.join(',');

    return "$"+first.join('.');

}

Link to comment
https://forums.phpfreaks.com/topic/97000-php-math-help/
Share on other sites

try

<?php
$num = 50000000;
function calculate($num){
$num = preg_replace('/[^0-9\.]/','',$num);
$tax_on = 0;
$tax_to = 0;
$cla=array(400000,250000,55000,0);
$rate_to = array(0.02,0.01,0.01,0.005);
$rate_on = array(0.02,0.015,0.01,0.005);
foreach ($cla as $k => $v){
	$x = $num > $v ? $num - $v: 0;
	$num -= $x;
	$tax_on += $x * $rate_on[$k];
	$tax_to += $x * $rate_to[$k];
}
return $tax_on + $tax_to;
}
function calculate_com($num){
$num = preg_replace('/[^0-9\.]/','',$num);
$tax_on = 0;
$tax_to = 0;
$cla=array(40000000, 400000,250000,55000,0);
$rate_to = array(0.01,0.015,0.01,0.01,0.005);
$rate_on = array(0.015,0.015,0.015,0.01,0.005);
foreach ($cla as $k => $v){
	$x = $num > $v ? $num - $v: 0;
	$num -= $x;
	$tax_on += $x * $rate_on[$k];
	$tax_to += $x * $rate_to[$k];
}
return $tax_on + $tax_to;
}
echo 'calculate -> $'.number_format(calculate($num),2),"<br />\n";
echo 'calculate_com -> $'.number_format(calculate_com($num),2),"<br />\n";
?>

Link to comment
https://forums.phpfreaks.com/topic/97000-php-math-help/#findComment-496788
Share on other sites

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.