PERFECT!! Works like a charm.
----- Original Message -----
From: "Scott Cadillac" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, September 19, 2003 10:35 AM
Subject: RE: Witango-Talk: [OT] Javascript eval()
Hi Brad,
The name is deceiving, but the JavaScript "eval()" function is not about
math. It' about executing "script" from dynamically created strings.
It's worth reading up on the "eval()" function, as well as the "Number()"
object.
Unfortunately, JavaScript has no native support for dealing with currency
style Math expressions, so for things like decimal precision or thousand
separator commas - you have to build your own function.
Searching Google.com will find you loads of these custom scripts, but below
is something that I modified from one of my own scripts that appears to work
with what you need. I think you'll also find my script is shorter than some
of the others you'll google.
-----
The script ignores any decimal places beyond 3 - but if a third decimal
digit is found, it will round it appropriately up to the integer value of
your sum.
So, your initial calculated "ext" sum of "0.995" will come out as "1.00"
And, for displaying currency, if the "ext" sum is a whole number, it will
show with two zero decimals, e.g., "4.00"
-----
And yes, it'll work with whatever dynamic number of <INPUT/> fields you have
on the page, as long as you specify the number in the JavaScript "for" loop
counter variable.
Note: I wrote and tested this with MSIE, but tried to write it so it is
cross-browser friendly. No guarantees, eh :-)
Hope this helps. Cheers......
<SCRIPT LANGUAGE='JavaScript'>
<!-- Raise the Bat shields!
function calculateLine(){
// for this script I hard coded the 2
// var rows=@@local$rowcnt
var taxLineArray = new Array();
for (var i = 0; i < 2 ; i++){
cnt = i +1;
var prodTotal_obj = new Object();
var eval_string1 = 'prodTotal_obj =
document.bform.prodTotal' + cnt;
eval(eval_string1);
var prodTotal_num = new Number(prodTotal_obj.value)
var unit_obj = new Object();
var eval_string2 = 'unit_obj = document.bform.unit' + cnt;
eval(eval_string2);
var unit_num = new Number(unit_obj.value)
var extTotal_obj = new Object();
var eval_string3 = 'extTotal_obj = document.bform.extTotal'
+ cnt;
eval(eval_string3);
var tmp_extTotal = prodTotal_num * unit_num;
extTotal_obj.value = set_precision(tmp_extTotal, 2);
}
}
function set_precision(tmp_num, decimal_places){
var tmp_num_as_string = tmp_num.toString();
var tmp_array = tmp_num_as_string.split('.');
var decimal_digits;
if(tmp_array.length == 2){
decimal_digits = tmp_array[1];
if(decimal_digits.length == 1){
decimal_digits += '0';
}else if(decimal_digits.length >= 3){
var decimals_to_keep = new
Number(decimal_digits.substr(0, 2))
var decimal_to_round = new
Number(tmp_array[1].substr(decimal_places, 1));
if(decimal_to_round >= 5){
decimals_to_keep++;
}
if(decimals_to_keep == 100){
var tmp_int = new Number(tmp_array[0]);
tmp_int++;
tmp_array[0] = tmp_int;
decimal_digits = new String('00');
}else{
decimal_digits = decimals_to_keep;
}
}
return tmp_array[0] + '.' + decimal_digits;
}else{
return tmp_num + '.00';
}
}
// down shields! -->
</SCRIPT>
<FORM NAME="bform" ACTION="" METHOD=POST >
<input type=text value="1.99" name="prodTotal1">
<input type=text value=".5" name="unit1" onBlur='calculateLine();'>
<input type=text value="" name="extTotal1">
<P>
<input type=text value="1.99" name="prodTotal2">
<input type=text value=".5" name="unit2" onBlur='calculateLine();'>
<input type=text value="" name="extTotal2">
</form>
Scott Cadillac,
Witango.org - http://witango.org
403-281-6090 - [EMAIL PROTECTED]
--
Information for the Witango Developer Community
---------------------
XML-Extranet - http://xmlx.ca
403-281-6090 - [EMAIL PROTECTED]
--
Well-formed Development (for hire)
---------------------
-----Original Message-----
From: Brad Robertson [mailto:[EMAIL PROTECTED]
Sent: Friday, September 19, 2003 3:34 AM
To: [EMAIL PROTECTED]
Subject: Witango-Talk: [OT] Javascript eval()
I wrote a simple javascript to calculate unit * qty and produce a line for
each row that is dynamically created. The problem is that I can't get the
extTotal value formatted as 2 digit decimal. Apparently, the eval()
function doesn't take any precision nor can I call a function from within
the eval function. I am beating my head against the computer trying to
figure out what seems relatively simple. Any javascripts gurus have any
thoughts???
<SCRIPT LANGUAGE='JavaScript'>
<!-- Raise the Bat shields!
function calculateLine()
{
// for this script I hard coded the 2
// var rows=@@local$rowcnt
var taxLineArray = new Array();
for (var i = 0; i < 2 ; i++)
{
cnt = i +1;
taxLineArray[i] =
(eval("document.bform.prodTotal" + cnt + ".value") *
eval("document.bform.unit" + cnt + ".value"));
eval("document.bform.extTotal" + cnt +
".value=" + taxLineArray[i]);
}
}
// down shields! -->
</SCRIPT>
<FORM NAME="bform" ACTION="" METHOD=POST >
<input type=text value="1.99" name="prodTotal1">
<input type=text value=".5" name="unit1" onBlur='calculateLine();'>
<input type=text value="" name="extTotal1">
<P>
<input type=text value="1.99" name="prodTotal2">
<input type=text value=".5" name="unit2" onBlur='calculateLine();'>
<input type=text value="" name="extTotal2">
</form>
I have tried to format the number before the eval() function, but the eval
function looses the function. I have also tried calling the following
functions..
function cent(amount){
return (amount == Math.floor(amount)) ? amount + '.00' : (
(amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
function clnNum(amount) {
return cent(Math.round(amount*Math.pow(10,2))/Math.pow(10,2));
}
________________________________________________________________________
TO UNSUBSCRIBE: Go to http://www.witango.com/maillist.taf
________________________________________________________________________
TO UNSUBSCRIBE: Go to http://www.witango.com/maillist.taf
________________________________________________________________________
TO UNSUBSCRIBE: Go to http://www.witango.com/maillist.taf
________________________________________________________________________
TO UNSUBSCRIBE: Go to http://www.witango.com/maillist.taf