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

check to see if a number is whole (ie no decimals)

Status
Not open for further replies.

zippynz

Programmer
May 17, 2001
50
NZ
Hi I am using a script to add up values of a form.
the values are dollar variables,
the first entry is for 20 cent pieces the second for 50 cents and so on (im from nz so yes we do have these values here)

before the script adds up the amount worth i want to check to see whether or not the value entered is a multiple of the currency,
for exampe if someone entered 1 dollar in the 20 cent form it would be correct,
but if some one entered 90 cents in the form it would be false,

im guessin you could just divide the value entered by the value of the currency and check to see whether it is a whole number but i have no idea how to do this,

can anyone help?
thanks :)
 
You can try the modulo operator (%) this returns the remainder in integer division. Something like this:

<SCRIPT>
var denomination = 20;
var remainder = 0;
var amount = parseInt(document.forms[0].Cents.value);
remainder = amount % denomination;

if(remainder != 0)
alert('Wrong amount entered');
</SCRIPT>

Of course, you can do whatever you want once you have the remainder but you get the idea. You can also make this into a function and pass the amount entered and the denomination to check for to it. That way you only need to write it once.

Hope this helps.
 
ok that was really helpful!!

and after i modified it, it works fine as long as the amount is less than 1 and not 0.6 (this is for validating 20cent pieces), i dont get why it works for some values and not others?

here is what i changed the code to:

Code:
function validate(){
var denomination = 0.20;
var remainder = 0;
var amount = parseFloat(document.frmMain.cents20.value);
remainder = amount % denomination;

if(remainder != 0)

alert('Wrong amount entered');
}

any ideas?
 
The modulo operator (%) returns the remainder for integer division. According to my reference book, you can use it with double and float values but, in playing with your code, I got some strange results.

You may need to convert your values to integers. Maybe something like this:

function validate(){
var denomination = 20;
var remainder = 0;
var amount = parseInt(document.frmMain.cents20.value * 100);
remainder = amount % denomination;

if(remainder != 0)

alert('Wrong amount entered');
}

This would convert your dollars to cents before getting the remainder and should work for any dollar amount entered.
 
sweet!

works like a charm thanks a lot :)
(and i need it when i wake up tomorrow to show a client :) good timing!)
 
ok weird but im still having trouble with this script,
their is the odd number which doesnt multiply properly, 64.6 is one of these
according to this script 64.6 * 100 = 6459
this is obviously false,
does anybody know why this would be?
anyone got a fix for it?
 
I use the script below to check an input field to see if it's numeric and no more than two decimal places.

function numbercheck(checkfield) {
if (isNaN(checkfield)) {
return false;
} else {
return true;
}
}

function decimalcheck(checkfield1) {
twodec=checkfield1.lastIndexOf('.');
fulllength=checkfield1.length;
if (twodec>=0 && parseInt(fulllength)-parseInt(twodec)<4) {
return true; //two decimal places only.
} else if(parseInt(twodec)<0) {
return true; //no decimal places integer.
} else {
return false; //more than two decimal places.
}
}


:-S
Regards
 
Thanks but thats not really what I am trying to do,
I need to check to see whether the value that is entered is a multiple of the denomination that value belongs to,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top