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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need assistance dealing with "0" value in calculation

Status
Not open for further replies.
Sep 12, 2007
143
US
Hi All, I need help dealing with a "0" value and division. I have 4 fields in a form that take a value from another field and either divide that value by 4, or return a "0" value. I tried to trick it by assigning a variable as "1", but this is not working correctly and I am not a JS programmer, more of an at-need wannabe. So...Here is the script (var b is one of each of 4 fields, so that changes), any assistance would be invaluable!

var a = this.getField("TotalCommission2");
var b = this.getField("Up1");
//or Demo1, Contract1, etc
if ((b.value == 0)) {
event.value = 0
} else {
var d = (a.value/4)
var c = 1
event.value = d*c
}
 
The problem is that "0" is not the same as 0. When you pull info out of a text field it is automatically treated as a string. If you want to make numeric comparisons to that string, you must first convert it to an integer or float (if it has a decimal). The javascript functions to do this are parseInt and parseFloat, respectively.

Try making this change to your code:
Code:
var a = this.getField("TotalCommission2");
var b = this.getField("Up1");
//or Demo1, Contract1, etc
if (([!]parseInt([/!]b.value[!], 10)[/!] == 0)) {
event.value = 0
} else {
var d = ([!]parseInt([/!]a.value[!], 10)[/!]/4)
var c = 1
event.value = d*c
}

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
kaht, thanks! One thing, you mentioned a decimal, these values definitely have a decimalm as they are dollars. Would this be better? (hat tip to MikeyJudd for showing me the syntax on another thread):

var a = parseFloat(this.getField("TotalCommission2").value);
var b = parseFloat(this.getField("Up1").value);
//or Demo1, Contract1, etc
if ((parseInt(b.value, 10) == 0)) {
event.value = 0
} else {
var d = (parseInt(a.value, 10)/4)
var c = 1
event.value = d*c
}
 
A question that pops into my head is if the value is entered by a user?

If so, I would account for human error and do a check to make sure the value is a number.

Code:
var a = this.getField("TotalCommission2");
var b = this.getField("Up1");
//or Demo1, Contract1, etc

var aInt = parseInt(a.value, 10);
var bInt = parseInt(b.value, 10);

if (!isNaN(aInt) && !isNaN(bInt)) {
    if (bInt == 0)) {
        event.value = 0
    } else {
        var d = (parseInt(a.value, 10)/4);
        var c = 1;
        event.value = d*c;
    } 
}else {
    alert('Value entered is not a valid number');
}
 
bigblock454, a slight change to your code:
Code:
var a = parseFloat(this.getField("TotalCommission2").value);
var b = parseFloat(this.getField("Up1").value);
//or Demo1, Contract1, etc
if (([s]parseInt([/s][!]b[/!][s].value, 10)[/s] == 0)) {
event.value = 0
} else {
var d = ([s]parseInt([/s][!]a[/!][s].value, 10)[/s]/4)
var c = 1
event.value = d*c
}

If you parse the values as they are assigned as floats, make sure that you don't convert them to integers later down in the page - that would eliminate the whole point of converting them to floats in the first place.

MikeyJudd also brings up a good point - it is a good idea to check for NaN (not a number) to ensure that your code doesn't crash as a result of non-numeric values being entered.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Ok, I am a little lost on the integer vs float values. All values on this form are dollars with the exception of the var b values, which are normally either a "0" indicating that there is no payment due for that 25% of the overall commission due, or a "1" indicating that the value is 25% of the commission due. It POTENTIALLY could be a value of .5, or .25 as well, but that would be a rarity. I agree that user error is always a factor, so the a;ert is a good thing!
 
Ok, I am a little lost on the integer vs float values.

Integer values:

1, 69, 78199101, 0, -69

Float Values:
1, -123, 5.681, 69.69, -.00001

Basically, if it has a decimal it needs to be defined as a float, if it doesn't need a decimal, then it *can* be defined as an integer - but could also be a float.



-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
OK, thanks. Now, can you explain what the 10 is for on the lines:


if ((parseInt(b.value, 10) == 0)) {
event.value = 0
} else {
var d = (parseInt(a.value, 10)/4)

and would I modify them to be:

if ((parseFloat(b.value, 10) == 0)) {
event.value = 0
} else {
var d = (parseFloat(a.value, 10)/4)
 
Hi again, I tried the following code and get a syntax error for the 10th line "event.value = 0" :

var a = this.getField("TotalCommission2");
var b = this.getField("Up1");
//or Demo1, Contract1, etc

var aInt = parseInt(a.value, 10);
var bInt = parseInt(b.value, 10);

if (!isNaN(aInt) && !isNaN(bInt)) {
if (bInt == 0)) {
event.value = 0
} else {
var d = (parseInt(a.value, 10)/4);
var c = 1;
event.value = d*c;
}
}else {
alert('Value entered is not a valid number');
}
 
Mikey, if I do it like this it still gives me a syntax error (I also tried it as "0"; & 0; still get the error):

var a = this.getField("TotalCommission2");
var b = this.getField("Up1");
//or Demo1, Contract1, etc

var aInt = parseInt(a.value, 10);
var bInt = parseInt(b.value, 10);

if (!isNaN(aInt) && !isNaN(bInt)) {
if (bInt == 0)) {
event.value = "0"
} else {
var d = (parseInt(a.value, 10)/4);
var c = 1;
event.value = d*c;
}
}else {
alert('Value entered is not a valid number');
}


 
can you explain what the 10 is for on the lines


It's the radix value for number. Essentially it determines if the number is binary, octal, decimal, hexadecimal, etc. Providing a value of 10 explicitly states that you're parsing a base-10 (decimal) number. 99 times out of 100 the 10 isn't needed, but if you read the link I posted you'll see "If the string begins with "0", the radix is 8 (octal)." This means that if a user types in a number like "0123456" and you parseInt it w/o specifying base-10, you're going to get very unexpected results. Try it for yourself:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

window.onload = function () {
   var a = "01234";
   alert(parseInt(a));
   alert(parseInt(a, 10));
};

</script>
<style type="text/css"></style>
</head>
<body>
</body>
</html>




-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Can you paste the syntax error into here?

Also, don't forget the semi-colon.

Code:
event.value = '0';
 
kaht, ok, I just stuck in an editor and it first returned a 668, and then a 1234. My script is for a fillable PDF though, not a webpage :) I'll read the link!
 
hi!

I've looked at your code some times now and have some questions.

¤ What is the event.value?
Javascript should through an error here because "event" is a predifined object for events and it doesn't have a property "value". So you should change the "event" to something else..

¤ What is the this.getField()?
Is "getField" a function you wrote? And how is the script triggered? Because the "this" can only be accessed like:

Code:
On the element:
<input onchange="alert(this.value);">

In a object:
var obj = {
          a_value:'hello',
          aMethod:function ()
                  {
                  alert(this.a_value);
                  }
          };

but not in functions..
well, it they are not triggered with new:
var a = new aFunction();

And always use the parseFloat when dealing with money.
 
Here is how I would write it if I understood what you want the function to do..

Code:
var a,b,ev;
a = parseFloat(document.forms[0]['TotalCommission2'].value);
b = parseFloat(document.forms[0]['Up1'].value);

if (b == 0) {ev = 0;}
else {ev = a/4;}

It's hard to help when I don't really know what the code is intended to do..
 
lowet, why not condense it a little more:
Code:
var a = parseFloat(document.forms[0]['TotalCommission2'].value);
var b = parseFloat(document.forms[0]['Up1'].value);
var ev = ((b) ? a : 0) / 4;

This, of course, takes out any kind of data integrity check and assumes that the values entered are numeric.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I still think you should check the value with isNaN due to users typing this in manually. Humans make mistakes... I call this ID-10-T errors. :eek:)
 
Yes, kaht, that's also a possibility. I just wanted him to easyly read the code. I use to use the ?: statements very often instead of the if/else in my scripts..

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top