Helpful Information
 
 
Category: JavaScript Development
Fixing the Javascript decimal problem

Hello,

I need some assistance with getting my Javascript program to work the way I need it too.

I have 3 different selections on a form, the first will let the user pick from different host plans, the second will allow the user to pick their payment plan, 12 or 6 months, the third will let them define who will register their domain name, myself or them.

My Javascript program takes the values from HOSTPLAN and multiplies that to the value in PAYMENT then we add on REGISTER. I send this to a location.href URL to PayPal.Com, where the user can pay by credit card. With PayPal, you specify the ammount to charge the person in the URL that goes to their secure server. I can't get Javascript to move the decimial. For instance, if the total is $133.40 in writes it as 133.4 and this confuses PayPal. Paypal wants 133.40.

Heres my script, any help would be appreciated.<SCRIPT language="JavaScript">

function AddPrice(){
var item1 = document.MATT.HOSTPLAN.value;
{
var item2 = document.MATT.PAYMENT.value;
{
var item3 = document.MATT.REGISTER.value;
{
total = eval((document.MATT.HOSTPLAN.value) * eval(document.MATT.PAYMENT.value)) + eval(document.MATT.REGISTER.value);

{
var riggedURL = "https://secure.paypal.x.com/xclick/business=webmaster%40pwrpage.net&item_name=PowerBasic&item_number=005&amount=" + total + "&shipping=0.00&return=http%3A//www.pwrpage.net";
location.href = riggedURL;
}}}}}

</SCRIPT>
</HEAD>
<BODY>
<FORM name="MATT" method=post ACTION="/cgi-sys/formmail.pl">
<INPUT type="hidden" name="recipient" value="webmaster@pwrpage.net">
<SELECT name="HOSTPLAN" size=1>
<OPTION selected value="6.95">PowerBasic $6.95/mo
<OPTION value="10.95">PowerBusiness $10.95/mo
<OPTION value="16.95">PowerMerchant $16.95/mo
<OPTION value="21.95">PowerEnterprise $21.95/mo
</SELECT>
<SELECT name="PAYMENT" size=1>
<OPTION selected value="12">12 Month
<OPTION value="6">6 Month
</SELECT>
<SELECT name="REGISTER" size=1>
<OPTION selected value="50.00">PowerPage
<OPTION value="0.00">Transfer
<OPTION value="0.00">Myself
</SELECT>
<INPUT TYPE="button" value="Proceed" onClick="AddPrice()">
</FORM>



[This message has been edited by mwtallman (edited August 18, 2000).]

Just convert your decimal to a string and check it for format (i.e. xxx.xx where x is a number).
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
var amt_str = amount.toString();
var dec_chk = /d*.d{2}/;
if( dec_chk.exec( amt_str ) != null ) {
// calid decimal currency value here
}
else {
// could try to fix it up, or write an error to the user. For now, alert() so we can see what went wrong...
alert( amt_str );
}
[/code]

Never used that code before so it may not work -- should be easy enough to modify to get it going properly though. One word of warning -- remember that users can bugger about with JS (and have it turned off), so anything that's coming off this form is (a) not secure and (b) not guaranteed to work. You'd be far better off actioning the redirect from the server side -- there's a slight performance hit (one more round trip to server), but at least your guaranteed to have it work.

Reviving an old thread.....

I was having a problem with a javascript converting a number with one decimal place to a string. seems that if the number after the decimal was a zero (this was for a time script where I was using hours and minutes to the tenth of the minute and a zero is a significant number), javascript was dropping the number and only converting integers above zero (ie 4.3 became "4.3" but 7.0 became "7"). After reading through this post, it occured to me that I could use a regex to check the string for a value after the period and if not, append the ".0" to the end as such:


// I am setting a number here for demo purpose
var my_num = 14.0;
var my_num_str = my_num.toString();
var reg1 = /\d*\.\d{1}/;

if (reg1.test(my_num_str))
{
// this block is if your string matches the regex
} else {
// this block is if your string does not match the regex
// ie. the ".0" has been removed by the .toString method
my_num_str = my_num_str + ".0"; // this adds back the ".0"
// my_num_str is now "14.0"
}

I have only revived this thread in hopes of helping someone else out there. The previous answer pointed me in the right direction, but it still took me a while to get the problem solved for my situation.

Regards,

Dave

Reviving an old thread.....
...but it still took me a while to get the problem solved for my situation.


A while... 6 years!!! :p










privacy (GDPR)