Click to See Complete Forum and Search --> : How do I reflect a value to a currency format?


DarryBoy
04-22-2003, 06:53 PM
I need to return the value that is calculated in the following script to a currency format (all credit to Jona). Please show me.

<html><head>
<script>
function calc(){
var chkd;
var count = 0;
if(document.qtyFrm.c1.checked){chkd=document.qtyFrm.c1.value; count=count+1;}
if(document.qtyFrm.c2.checked){chkd=document.qtyFrm.c2.value; count=count+1;}
if(count==2){alert("Check only one, please."); return false;}
document.qtyFrm.qtyTotal.value=parseInt(document.qtyFrm.qty.value)*chkd;
}
</script>
</head><body>
<form name="qtyFrm">
<input type=text name="qty" onKeyPress="calc()">
<br>
130: <input type=checkbox name="c1" value="130" onclick="return calc()"><br>
1300: <input type=checkbox name="c2" value="1300" onclick="return calc()"><br>
<input type=text name="qtyTotal">
</form></body></html>

Kind regards
Darryn

Jona
04-22-2003, 07:09 PM
Did you only want that in dollars? Or include cents, too?

<html><head>
<script>
function calc(){
var chkd;
var count = 0;
if(document.qtyFrm.c1.checked){chkd=document.qtyFrm.c1.value; count=count+1;}
if(document.qtyFrm.c2.checked){chkd=document.qtyFrm.c2.value; count=count+1;}
if(count==2){alert("Check only one, please."); return false;}
document.qtyFrm.qtyTotal.value=parseInt(document.qtyFrm.qty.value)*chkd;
if(!document.qtyFrm.qtyTotal.value.indexOf("$")){ document.qtyFrm.qtyTotal.value="$"+document.qtyFrm.qtyTotal.value;}
}
</script>
</head><body>
<form name="qtyFrm">
<input type=text name="qty" onKeyPress="calc()">
<br>
130: <input type=checkbox name="c1" value="130" onclick="return calc()"><br>
1300: <input type=checkbox name="c2" value="1300" onclick="return calc()"><br>
<input type=text name="qtyTotal">
</form></body></html>

DarryBoy
04-22-2003, 07:17 PM
Thanx Jona. Dollars and cents. The code you have adjusted for me doesn't reflect the value as a currency.

Jona
04-22-2003, 07:22 PM
Oops, my mistake. Use this:

<html><head>
<script>
function calc(){
var chkd;
var count = 0;
if(document.qtyFrm.c1.checked){chkd=document.qtyFrm.c1.value; count=count+1;}
if(document.qtyFrm.c2.checked){chkd=document.qtyFrm.c2.value; count=count+1;}
if(count==2){alert("Check only one, please."); return false;}
document.qtyFrm.qtyTotal.value=parseInt(document.qtyFrm.qty.value)*chkd;
if(document.qtyFrm.qtyTotal.value.indexOf("$")){ document.qtyFrm.qtyTotal.value="$"+document.qtyFrm.qtyTotal.value+".00";}
}
</script>
</head><body>
<form name="qtyFrm">
<input type=text name="qty" onKeyPress="calc()">
<br>
130: <input type=checkbox name="c1" value="130" onclick="return calc()"><br>
1300: <input type=checkbox name="c2" value="1300" onclick="return calc()"><br>
<input type=text name="qtyTotal">
</form></body></html>

DarryBoy
04-22-2003, 07:35 PM
Thank you Jona. It's perfect.