Re: Help ! Shopping Cart Problem



Phil McKraken wrote:
The problem is your dollar() function, here is a link to some 'to money' conversion stuff:


I tried the links you posted but, (: no luck. I am sure I am just
doing it wrong. I got this sample from one of those javascript web
repositories and I am not competent to change it to make it work. I am
still learning and am not really a js writer, only a "cut n paste"
trial & error modifier of what's there already.


Can someone tell me what to do to fix this a little more specifically.

You really need to understand what the functions are doing and how they work, else you will get errors like you have now. I hope you are validating everything back at the server and only using the shopping cart for advice to customers.


Anyhow, here's a couple of functions that may suit. They do no validation at all and expect you to pass them suitable input. The first expects to get dollars and never more than two decimal places, the second expects integer cents.



<html><head><title>Play</title>

<script type="text/javascript">

// This function expects to get dollars as either an integer
// or a float but never more than 2 decimal places
function toDollars(amt)
{
  if (0 == amt) return '0.00';
  amt += '';  // Convert amt to string
  amt = amt.split('.');
  if (1 == amt.length)    amt[1]='00';
  if (0 == amt[1].length) amt[1] += '00';
  if (1 == amt[1].length) amt[1] += '0';
  return amt.join('.');
}

// This function expects to get only integer cents
function centsToDollars(amt)
{
  amt += '';  // Convert amt to string
  if (1 == amt.length) return '0.0'+ amt;
  if (2 == amt.length) return '0.'+ amt;
  var dollars = amt.substring(0,amt.length-2);
  var cents   = amt.substring(amt.length-2);
  return dollars + '.' + cents;
}

</script>
</head><body>
Play with toDollars<br>
<input type="text" onblur="
  this.value = toDollars(this.value);
"><br>
Play with centsToDollars<br>
<input type="text" onblur="
  this.value = centsToDollars(this.value);
">

</body></html>


[...]

--
Rob
.