need a javascript for money field

A

Alex Shi

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
 
R

Reply Via Newsgroup

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;
}
 
D

Dr John Stockton

JRS: In article <QSkhc.179864$Pk3.41490@pd7tw1no>, seen in
Reply Via Newsgroup <reply-to-
(e-mail address removed)> posted at Wed, 21 Apr 2004 02:19:28 :
Alex Shi wrote:

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="javascript+faq"&hl=en&lr=&ie=UTF-
8&oe=UTF-8&scoring=d&selm=4083bed2%240%2431712%24fa0fcedb%40lovejoy.zen.co.uk&rn
um=1

No, the proper recommendation is to look in the newsgroup itself, where
the relevant part is posted on Mon & Fri; or to give the actual URL,
which is posted here much more frequently.

Below is a function I wrote myself
...
function roundTo2decimal(X)
{
var tmpA=parseInt(X*100);

That multiplies the number X by 100, converts it to string, and converts
it back. What for?
var leftSide=Math.floor(X);
var pounds=leftSide.toString(); // Left side as a string
////////////////////////////////////////////////////////////////////////////////
//
var rightSide=parseInt(tmpA - (leftSide*100));

Ditto re parseInt.
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) )

Are both tests needed?
{ 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"; }

Is that ever executed?
////////////////////////////////////////////////////////////////////////////////
//
// Return X and ensure our pennies string is no greater than two
// characters in length
X=pounds+"."+pennies.substring(0,2);
return X;
}


Although it has Math.round, roundTo2decimal(0.9999999999999999) -> 0.99

roundTo2decimal(-0.00000000001) -> -1.91 - note that float arithmetic is
inexact.

roundTo2decimal(0.00000000001) -> 0.09

roundTo2decimal(100.0000000000001) -> 100.09

It is the second, unnecessary parseInt that does much of the damage.


The OP would be better advised to use the method in the FAQ. Never
trust anything posted anonymously. Never trust anything posted.
 
A

Alex Shi

Hi,

Thanks for your help!
I already found an easy one from google, and it works perfect:

function myRound(field)
{
sm = "" + ((Math.round(field.value * 100)) / 100);

dec1 = sm.substring(sm.length-3, sm.length-2);
dec2 = sm.substring(sm.length-2, sm.length-1);

if (dec1 != '.') { // adds trailing zeroes if necessary
if (dec2 == '.')
sm += "0";
else
sm += ".00";
}
field.value = sm;
}



Alex
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
I already found an easy one from google, and it works perfect:

function myRound(field)
{
sm = "" + ((Math.round(field.value * 100)) / 100);

dec1 = sm.substring(sm.length-3, sm.length-2);
dec2 = sm.substring(sm.length-2, sm.length-1);

if (dec1 != '.') { // adds trailing zeroes if necessary
if (dec2 == '.')
sm += "0";
else
sm += ".00";
}
field.value = sm;
}

That, adapted for general use, reduces to

function aRound(X) { var S, P
S = String((Math.round(X * 100)) / 100)
P = S.length-3
if (S.charAt(P) != '.') S += (S.charAt(++P) == '.') ? "0" : ".00"
return S }


Other than lacking a test on X being too big, ISTM OK --
use if (/e/.test(S)) { return ''+X } // cannot cope

The FAQ version, however, allows for padding on the left, needed in
tabulation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top