Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ParseFloat problem and NaN result

Status
Not open for further replies.

lesj1968

Programmer
Sep 3, 2005
166
GB
Hi

I have a switch statement in a form I have created for a drop down list which has two options. My simple code is as follows:
-------------------------------------------------
var i = parseInt(event.srcElement.returnValue, 10);
var h = parseFloat(crmForm.netamount.value, 10);

switch(i)
{

case 1:
window.alert("Please click 'Calculate VAT' option below");
i=0.00;
break;

case 2:
i=parseFloat((h*1.175)-h, 10);
break;
}

parseFloat(crmForm.tax.value, 10) = i;

----------------------------------------------------------

The purpose of the drop down list is to calculate VAT (tax which is 17.5%) based on what the net amount is. The net amount field is called "netamount" and my VAT/tax field is called "tax".

When option 2 in the drop down list is clicked the VAT is calculated by multiplying netamount(h) by 1.175. The netamount(h) is then subtracted from this result to get the VAT separately.

Initially the tax field was getting populated with a NaN result, which I resolved by using ParseFloat on all the variables. However I have found a problem with my function above - it does not calculate (h) correctly. It thinks the value is 1000 times smaller than it actually is. For example if the netamount(h) in fact equals 6000 my function thinks it is 6. Also, it seems to ignore the last 3 digits to the left of the number... it only sees the first digit on the left for example even if the number is 6750 or 6900 it still sees 6. If the netamount for example equals 21500, my function will only see 21. It seems to be 3 significant places out (if that is the term).

Please can anyone see where I am going wrong. I assume the problem may lie with the fact I am using parseInt for the variable (i) at the top. However I was lead to believe I need to use this as the drop down list ID values are either 1 or 2 for each option, both being integers, therefore I need parseInt (this is in the first line of the code where I declare (i) )

Many thanks for any help resolving this problem.

Les
 
Some minor alterations, but nothing major...
Code:
var i = parseInt(event.srcElement.returnValue, 10);
var h = parseFloat(crmForm.netamount.value, 10);
switch(i) {
  case 1:
    window.alert("Please click 'Calculate VAT' option below");
    i=0;
    break;
  case 2:
    i=parseFloat(h*0.175, 10);
    break;
}
crmForm.tax.value = i;
Let me know if this works in-situ.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hi Thanks for your reply. Unfortunately the netamount(h) is still being read incorrectly for some reason...the last three significant places ( to the left of the decimal point) are being ignored completely ... What could cause this?

Also, once I have solved this problem I will be adding an IF statement in case the netamount field is empty or null. Is the following IF statement correct:

if (crmForm.netamount.value != null)
{
code
}



Or is NULL declared in a different way?

Thanks again.
 
When you get the contents of a form field by getting the data from the "value" of the input element... the result is a string. That is why you are "casting" into a number.

So, as a means of testing your code, you can replace the code that gets the data, and instead hard code it:
Code:
// var i = parseInt(event.srcElement.returnValue, 10);
// var h = parseFloat(crmForm.netamount.value, 10);
var i = parseInt("2", 10);
var h = parseFloat("1234", 10);

switch(i) {
  case 1:
    window.alert("Please click 'Calculate VAT' option below");
    i=0;
    break;
  case 2:
    i=parseFloat(h*0.175, 10);
    break;
}
crmForm.tax.value = i;
This will allow you to break-down the point at which you are getting odd data back. You may want to cut down the page until just the bare minimum to do this task is present... and post the HTML if you are still having problems.

Regarding your other question - just test for empty string instead of null:
Code:
if (crmForm.netamount.value != '') {

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hi

Thanks for your reply. The testing allowed me to narrow it down to the (h) netamount variable declaration, in particular:

crmForm.netamount.value

This should return the value that is held in the netamount field, instead it still ignores 3 significant places, eg. if the actual netamount is 35500.60, it will only see 35.

Do you know what might be causing this?

Everything else works, including the IF statement and the "i" variable declaration.

Many thanks.

Les
 
Hi

I have worked out what is causing my problem. The problem is the "," in my netamount values. When I parseFloat the values it only reads up to the
",". Is there a function in Javascript that will search and remove specified characters in a string?

Thanks again.

 
That's great news on figuring that out.

Whilst there isn't a function specifically, you can use the following to remove your comma:
Code:
var h="12,345.21";
h = h.replace(',','');
alert(h); // 12345.21

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Many thanks for your help. I'll give that a go in the morning.
 
Hi

It works... thanks for your help with this.

In the IF statement in case 2, if the taxamount field is empty the else statement is called alerting the user to save the record before calculating VAT. How can I make
the drop down list ID value (which will currently be ID=2 as case 2 has been carried out) revert to the first option in the drop down list (i.e ID 1).
I need it to revert back to the first option where the drop down list ID = 1. The name of the drop down list field is "calculate_vat".

I have tried:
....
else
{
window.alert("You must SAVE the record before calculating VAT.");
i=0.00;
crmForm.calculate_vat.reset();
}
break;
....

Many thanks for any help and information.
 
This will set the select dropdown to the first option (remember that all arrays start with 0 as their first item):
Code:
crmForm.calculate_vat.options[0].selected=true;

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hi thanks for your reply. Unfortunately I have tried what you suggested and it doesn't work for some reason. Are there any alternatives to that solution?
 
The following small test page shows this working for me in the following browsers (I had a test suite handy):
Opera 6,7,8; Netscape 7,8; IE 5,5.5,6; Firefox; Mozilla 1.7

Code:
<html>
<head><title></title>
<script type="text/javascript">
function resetFunction() {
	document.myForm.mySelect.options[0].selected=true;
}
</script>
</head>
<body>
<form name="myForm" action="">
<fieldset>
	<select name="mySelect">
		<option value="1">Option 1</option>
		<option value="2">Option 2</option>
	</select>
</fieldset>
</form>
<a href="javascript:resetFunction()">Reset select</a>
</body>
</html>

It could be that your markup for the calculate_vat drop down is not correct (check for closing tags etc). It could be a typo somewhere. It's odd that you can read the value of the dropdown, but not set it though.

Anyway, take a look at the example I've posted and see if there is any major differences between that and your code.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top