surfertide
Technical User
Hello:
I have this javascript code where I can find the percentage (2%, 2.5%, 3%) of a sales price. It works out fine but I would like to modify the code so when I type E.g: 500,000.00 the values would show up. At this moment it doesn't work with the format above. It only works if I type E.g: 500000
Is there a way to make it work if I put commas and a period for the decimals?
An example page could be found at:
13west.com/test/
Here's the javascript code:
<script Language="JavaScript">
<!--Hide From Other Browsers
function tips(form,changed){
var tip = form.tip.value;
if (changed == "tip"){
var two = tip * 0.02;
var twofive = (tip * 0.025);
var three = (tip * 0.03);
tip = formatCurrency(tip);
form.tip.value = tip;
}
form.two.value = formatCurrency(two);
form.twofive.value = formatCurrency(twofive);
form.three.value = formatCurrency(three);
}
function formatCurrency(num) // got most of this from a javascript example site
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
//Stop Hiding -->
</script>
I have this javascript code where I can find the percentage (2%, 2.5%, 3%) of a sales price. It works out fine but I would like to modify the code so when I type E.g: 500,000.00 the values would show up. At this moment it doesn't work with the format above. It only works if I type E.g: 500000
Is there a way to make it work if I put commas and a period for the decimals?
An example page could be found at:
13west.com/test/
Here's the javascript code:
<script Language="JavaScript">
<!--Hide From Other Browsers
function tips(form,changed){
var tip = form.tip.value;
if (changed == "tip"){
var two = tip * 0.02;
var twofive = (tip * 0.025);
var three = (tip * 0.03);
tip = formatCurrency(tip);
form.tip.value = tip;
}
form.two.value = formatCurrency(two);
form.twofive.value = formatCurrency(twofive);
form.three.value = formatCurrency(three);
}
function formatCurrency(num) // got most of this from a javascript example site
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
//Stop Hiding -->
</script>