Alex said:
Hi,
I'm working on a form integrated with a payment gateway.
The payment company requires that the amount must contains
exactly two decimal digits, say, 1.00, 1000.00, 1.20, 1.25, etc.
12.5 will be treated as 1.25. So I need a javascript to do
- check if input value is numeric
- pad '0' if there're less than 2 digits of decimal.
Thanks in advance!
Alex
I suggest you have a look at the FAQ as its a common problem, and more
than just the loss of the trailing zero. Go to google.groups.com and
search for "javascript faq" and sort by date - alternativly, try
clicking on the link below:
http://groups.google.com/groups?q="[email protected]&rnum=1
Below is a function I wrote myself - I'm proud of it because the
suggested solution in the FAQ was too complicated for me and I've only
got about six months or more javascripting under my belt so this was a
small, but significant first step for me.
I've got plenty of comments in it but make sure you test it - I've not
shared this code with anybody up until now, but I've found previous
numbers that gave me problems no longer do so, and I take reasonable
comfort in believeing that it works safely... but... you choose if you
want to use it - I just ask that you keep my name in the function (and
share any changes you make to it with others in the newsgroup).
There are a couple of considerations you should be aware off in case you
are too lazy to read the FAQ - There is a difference between strings and
numeric variables from a mathimatical point of view. The value you get
from my function below will be a string (meaning "1.02" and if you were
to add 1 to it, you would get "1.021" and not "1.03"). I would suggest
you hide the real value of your calculations and use my function to read
your hidden field - Do *not* use this functions result as part of your
math calculations, but use it only for eye candy / display the result.
Can you understand the difference? It is an important one and if you
don't you'll end up with wierd results.
Hope it helps - and I'd be interested if other javascripters could
comment on it...
randelld
function roundTo2decimal(X)
{
//////////////////////////////////////////////////////////////////////////////////
// Function returns X as a string, rounded up to two decimal places
// By Randell D
http://www.fiprojects.com
//////////////////////////////////////////////////////////////////////////////////
// Rounding to two decimal places can be problematic when you combine
// the burden of number to string conversion - My original testing found
// that some numbers, when limited to two decimal places gained
// additional digits - When this didn't happen any decimal that had a
// trailing zero led to the trailing zero (or zero's) being dropped
// I get around this by seperateing the left and right side of
// the decimal point and appending a trailing zero when the right side
// is less than two digits in length.
//////////////////////////////////////////////////////////////////////////////////
// We need to seperate our number in to two - leftside and right side of
// decimal - we cannot rely on using the decimal point as a seperator so
// instead, we compute the value of the pennies/cents/right side of
// decimal
var tmpA=parseInt(X*100);
var leftSide=Math.floor(X);
var pounds=leftSide.toString(); // Left side as a string
//////////////////////////////////////////////////////////////////////////////////
var rightSide=parseInt(tmpA - (leftSide*100));
var rightSide=Math.round(rightSide);
var pennies=rightSide.toString(); // Right side as a string
//////////////////////////////////////////////////////////////////////////////////
// If our pennies/cents are less than ten (for example, 1.02) then our
// zero can be missing - We check the length and preceed a zero if its
// missing
if( (rightSide<10) && (pennies.length<2) )
{ pennies="0"+pennies; }
//////////////////////////////////////////////////////////////////////////////////
// If the right side is greater than nine, and less than two characters
// in length, then its likely we have something like 1.2 instead of 1.20
// so we append a zero to the right side of our cents/pennies
if( ( rightSide>9 ) && (pennies.length<2) )
{ pennies=pennies+"0"; }
//////////////////////////////////////////////////////////////////////////////////
// Return X and ensure our pennies string is no greater than two
// characters in length
X=pounds+"."+pennies.substring(0,2);
return X;
}