Google Answers Logo
View Question
 
Q: Simple Java Calculator required. ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Simple Java Calculator required.
Category: Computers > Programming
Asked by: ronw-ga
List Price: $25.00
Posted: 15 May 2006 11:07 PDT
Expires: 14 Jun 2006 11:07 PDT
Question ID: 729045
I require this formula done in Javascript to insert on a Web
Page.  I want one user fillable field that says "Purchase Price" and a button
that says "Calculate" and a second field that will be filled with the
calculation and a text field next to it that says "Manitoba Land Transfer Tax
Fee".

I require these amounts and values to be calculated in the background
and then displayed in the text field next to "Land Transfer Tax Fee":

On the first $30,000  0% (so the "Land Transfer Tax Fee" should read $0)

On the next $60,000 (i.e. $30,001 to $90,000)  0.5% 

On the next $60,000 (i.e.$90,001 to $150,000)  1.0% 

On the next $50,000 (i.e. $150,001 to $200,000)  1.5% 

On amounts in excess of $200,000  2.0% 

Thanks for viewing !

rwersch@yahoo.com
Answer  
Subject: Re: Simple Java Calculator required.
Answered By: palitoy-ga on 15 May 2006 12:02 PDT
Rated:5 out of 5 stars
 
Hello ronw-ga,

Thank-you for your question.

I have written a small webpage for you that I think does what you
require, hopefully it should be simple to see how you can add it to
your existing webpage.

<html>
<head>
<script language="javascript">
function calculate_it(numb) {
	var total = 0;
	if ( numb > 30000 ) { 
		if ( numb > 90000 ) { total = total + 300; }
		else { total = total + ((numb-30000)*0.005); }
	}
	if ( numb >= 90000 ) { 
		if ( numb > 150000 ) { total = total + 600; }
		else { total = total + ((numb-90000)*0.01); }
	}
	if ( numb >= 150000 ) { 
		if ( numb > 200000 ) { total = total + 900; }
		else { total = total + ((numb-150000)*0.015); }
	}
	if ( numb >=200000 ) { total = total + ((numb-200000)*0.02); }
	document.forms.the_form.tax.value = total;
}
</script>
</head>

<body>
<form name="the_form" action="" method="get">
Purchase Price: <input name="price" type="text" /><br />
Manitoba Land Transfer Tax Fee: <input name="tax" type="text" /><br />
<input name="Calculate" value="Calculate" type="button"
onclick="calculate_it(document.forms.the_form.price.value);" />
</form>
</body>
</html>

It may require a little tweaking and if it does please do not hesitate
to ask for clarification and I will do my best to respond swiftly.  If
the mathematics are not correct it would be helpful if you could
provide me with a list of a few example answers (for example $55,000
should give a tax value of x).

Request for Answer Clarification by ronw-ga on 15 May 2006 12:12 PDT
Hi.

The only calculations that can be compared to the calculator can be found here:

http://www.gov.mb.ca/finance/fedprov/landtransfertax.html

As well, can the code be inserted in to a "table" (3 columns and 2
rows) please so it can be easily lined up on the page and a border can
be put around it then if I wish.

Thanks for your quick post.

Ron

Clarification of Answer by palitoy-ga on 15 May 2006 12:22 PDT
Hello Ron,

Let me know if this meets your needs:

<html>
<head>
<script language="javascript">
function calculate_it(numb) {
	var total = 0;
	if ( numb > 30000 ) { 
		if ( numb > 90000 ) { total = total + 300; }
		else { total = total + ((numb-30000)*0.005); }
	}
	if ( numb >= 90000 ) { 
		if ( numb > 150000 ) { total = total + 600; }
		else { total = total + ((numb-90000)*0.01); }
	}
	if ( numb >= 150000 ) { 
		if ( numb > 200000 ) { total = total + 900; }
		else { total = total + ((numb-150000)*0.015); }
	}
	if ( numb >=200000 ) { total = total + ((numb-200000)*0.02); }
	document.forms.the_form.tax.value = total;
}
</script>
</head>

<body>
<form name="the_form" action="" method="get">
<table width="100%" border="0">
  <tr>
    <td>Purchase Price:</td>
    <td><input name="price" type="text" /></td>
  </tr>
  <tr>
    <td>Manitoba Land Transfer Tax Fee:</td>
    <td><input name="tax" type="text" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input name="Calculate" value="Calculate" type="button"
onClick="calculate_it(document.forms.the_form.price.value);" />
    </div></td>
    </tr>
</table>
</form>
</body>
</html>

Request for Answer Clarification by ronw-ga on 15 May 2006 12:50 PDT
This is very very very close to perfect !!

One thing though.........if you enter an amount with a comma 25,000 it
will not calculate.

Can it be allowed in the back end to remove any $ sign entered and or
, in the amount placed by the user?

Thanks !!!

Request for Answer Clarification by ronw-ga on 15 May 2006 14:11 PDT
OOOOOOOOOOOPSSSSSS !!!!!!!!

Calculator is incorrect in prices over $200,000

Prices over $200,000 have 2% added to the sum total of the cost up to $200,000.

This might better explain it:

To calculate the total Manitoba Land Transfer Tax for residential properties,


ADD THE FOLLOWING TOGETHER: 

no tax on the first $30,000 of the purchase price

0.5% of the amount from $30,001 to $90,000

1.0% of the amount from $90,001 to $150,000

1.5% of the amount from $150,001 to $200,000

2.0% of the amount in excess of $200,000

Request for Answer Clarification by ronw-ga on 15 May 2006 14:14 PDT
Or this might be an easier calculation if it's easier to do:

Purchase Price   Calculation of Land Transfer Tax 

0 - 30,000   Not taxable 
30,001 - 90,000   (.005 x Amount) minus 150 
90,001 - 150,000   (.01 x Amount) minus 600 
150,001 - 200,000   (.015 x Amount) minus 1,350 
200,001 +   (.02 x Amount) minus 2,350

Clarification of Answer by palitoy-ga on 16 May 2006 01:38 PDT
Hi again Ron,

Sorry for not replying immediately, I am in the UK and I needed some sleep!

I have added the removal of dollar-signs and commas from the script as
you requested.

I altered the way it calculates things to your simplyfied method in
your last comment. I am glad you checked this as I wasn't sure the
answers I was calculating were correct as I have not experience in the
calculation of these taxes and hoped you had some examples that you
could test.   Can you please check these values again to ensure they
are correct?

In these calculations I have rounded the value to 2 decimal places but
this can easily be changed depending on how your tax system requires
these values to be presented.

Please let me know if there is anything else you require.

<html>
<head>
<script language="javascript">
function calculate_it(numb) {
	var total = 0;
	numb = numb.replace(/\,/,'');
	numb = numb.replace(/\$/,'');
	if ( numb > 30000 && numb <= 90000 ) { 
		total = (numb*0.005)-150;
	}
	if ( numb > 90000 && numb <= 150000 ) { 
		total = (numb*0.01)-600;
	}
	if ( numb > 150000 && numb <= 200000 ) { 
		total = (numb*0.015)-1350;
	}
	if ( numb >=200000 ) { total = (numb*0.02)-2350; }
	document.forms.the_form.tax.value = Math.round(total*100)/100;
}
</script>
</head>

<body>
<form name="the_form" action="" method="get">
<table width="100%" border="0">
  <tr>
    <td>Purchase Price:</td>
    <td><input name="price" type="text" /></td>
  </tr>
  <tr>
    <td>Manitoba Land Transfer Tax Fee:</td>
    <td><input name="tax" type="text" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input name="Calculate" value="Calculate" type="button"
onClick="calculate_it(document.forms.the_form.price.value);" />
    </div></td>
    </tr>
</table>
</form>
</body>
</html>

Clarification of Answer by palitoy-ga on 16 May 2006 01:40 PDT
There is a small typo in the last script I posted.  The following line:

if ( numb >=200000 ) { total = (numb*0.02)-2350; }

Should read:

if ( numb > 200000 ) { total = (numb*0.02)-2350; }

(as we only want numbers greater than 200000 to have this formula applied.)

Clarification of Answer by palitoy-ga on 16 May 2006 02:54 PDT
I have been testing this and have spotted another small error (when
properties have a value in excess of $1,000,000 or when more than one
comma is in the number an incorrect tax value would be given).

The revised script is this:

<html>
<head>
<script language="javascript">
function calculate_it(numb) {
	var total = 0;
	numb = numb.replace(/[^0-9\.]/g,'');
	if ( numb > 30000 && numb <= 90000 ) { 
		total = (numb*0.005)-150;
	}
	if ( numb > 90000 && numb <= 150000 ) { 
		total = (numb*0.01)-600;
	}
	if ( numb > 150000 && numb <= 200000 ) { 
		total = (numb*0.015)-1350;
	}
	if ( numb > 200000 ) { total = (numb*0.02)-2350; }
	document.forms.the_form.tax.value = Math.round(total*100)/100;
}
</script>
</head>

<body>
<form name="the_form" action="" method="get">
<table width="100%" border="0">
  <tr>
    <td>Purchase Price:</td>
    <td><input name="price" type="text" /></td>
  </tr>
  <tr>
    <td>Manitoba Land Transfer Tax Fee:</td>
    <td><input name="tax" type="text" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input name="Calculate" value="Calculate" type="button"
onClick="calculate_it(document.forms.the_form.price.value);" />
    </div></td>
    </tr>
</table>
</form>
</body>
</html>

Request for Answer Clarification by ronw-ga on 16 May 2006 06:34 PDT
Hi ya.

I am in Winnipeg, Manitoba..........which is GMT-6.

I will not be able to test your latest post until later this evening. 
I will do another post here once I have tested.

Many thanks for being so prompt.

Ron

Clarification of Answer by palitoy-ga on 16 May 2006 06:40 PDT
No problem, I will keep checking every hour or so (for the next 5-6
hours from posting this) and try to post any fixes you require today. 
You need only test the code in the last clarification I posted as this
includes the previous fixes.

We used to get update emails from Google when a customer requests a
clarification but this is being fixed at present at Google HQ so we
are having to do things manually during this period.  Thank-you for
your understanding and patience.

Request for Answer Clarification by ronw-ga on 16 May 2006 07:13 PDT
You can email me direct at rwersch@yahoo.com if you wish and if you
want to advise me of your email address that would be great also.

I will not be able to test the last script for approx 8 - 10 hours.

Ron

Clarification of Answer by palitoy-ga on 16 May 2006 08:54 PDT
Hello Ron,

Unfortunately it is against Google Answers rules to contact you
directly by email and all contact we have has to be through this
medium.  Hopefully this is not too much trouble for you, sometimes
this is frustrating for both parties as email can be a quicker and
easier medium to sort problems out through.

palitoy-ga

Request for Answer Clarification by ronw-ga on 16 May 2006 14:20 PDT
All is great !!

I have checked the calculations and for sure, it looks like the easier
to follow method of calculating is easier.

I will ask one more thing please.........(sorry if this is alot), but
when a number gets generated as an "answer" which is essentially a
dollar amount, I put a $ just before the field to indicate "dollars".

Could it be possible that the answer that gets generated has a "comma"
"," in it if the value goes over $999.   Example:  $1,000 instead of
$1000

Much appreciated.  I will leave a tip.

Here is the page in action:

http://www.joybuettner.ca/joy/landtransfertax.htm

Clarification of Answer by palitoy-ga on 17 May 2006 02:04 PDT
I have had to write a small extra function to format the number in the
way that you desired but I believe it is now producing the correct
formatting (it even works for taxes requiring more than one comma in
the number!).

Let me know if there is anything else you require.

<html>
<head>
<script language="javascript">
function calculate_it(numb) {
	var total = 0;
	numb = numb.replace(/[^0-9\.]/g,'');
	if ( numb > 30000 && numb <= 90000 ) { 
		total = (numb*0.005)-150;
	}
	if ( numb > 90000 && numb <= 150000 ) { 
		total = (numb*0.01)-600;
	}
	if ( numb > 150000 && numb <= 200000 ) { 
		total = (numb*0.015)-1350;
	}
	if ( numb > 200000 ) { total = (numb*0.02)-2350; }
	document.forms.the_form.tax.value = format(total,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('.');
}

</script>
</head>

<body>
<form name="the_form" action="" method="get">
<table width="100%" border="0">
  <tr>
    <td>Purchase Price:</td>
    <td><input name="price" type="text" /></td>
  </tr>
  <tr>
    <td>Manitoba Land Transfer Tax Fee:</td>
    <td><input name="tax" type="text" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input name="Calculate" value="Calculate" type="button"
onClick="calculate_it(document.forms.the_form.price.value);" />
    </div></td>
    </tr>
</table>
</form>
</body>
</html>
ronw-ga rated this answer:5 out of 5 stars and gave an additional tip of: $20.00
Amazing communication, great code, great "Google Answers" experience !!

Comments  
Subject: Re: Simple Java Calculator required.
From: palitoy-ga on 18 May 2006 02:15 PDT
 
Thank-you for the kind comments, 5-star rating and generous tip!  They
are all much appreciated.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy