M
Mick White
KsAdmin said:Ok figured it out,
function commaThis(entry) {
entry=Math.floor(entry)+""
var rex = /(-?\d+)(\d{3})/
while (rex.test(entry)) {
entry = entry.replace(rex, "$1,$2")
}
return entry;
}
instead of
return entry+getDecimal(entry);
// My mistake, the "getDecimal()" function assumes the argument passed
// to be a Number with decimals.
function getDecimal(val){
val+="";
d=val.indexOf(".");
return d==-1?"":val.substring(d);
}
// This takes care of any Numbers
Mick
Ok, i got this working like so....
function getDecimal(val){
val+="";
return val.substring(val.indexOf("."));
}
function commaThis(entry) {
entry=Math.floor(entry)+""
var rex = /(-?\d+)(\d{3})/
while (rex.test(entry)) {
entry = entry.replace(rex, "$1,$2")
}
return entry+getDecimal(entry);
}
//math portion
var fields=Array("life1","life2","life3","life4","life5","life6");
for(i=0;i<fields.length;i++){
str="var
val"+i+"=parseInt(document.frmLifeNeed."+fields+".value);";
eval(str);
}
//watch word wrap here
document.frmLifeNeed.total.value =
commaThis(val0+val1+val2+val3+val4-val5);
It displays the total with the proper commas now.... it just displays
the total twice..... ex. 4,5004,500 Instead of 4,500
any ideas?
KsAdmin wrote:
<snip>
I would like to change the format of the total from
1234567 to 1,234,567 . Is this possible using javascript? Here is the
code that I use to do the calculations. I have edited out all the code
that I use to validate the fields because I don't think it is needed
for this question.
// Use getDecimal() function to get the decimal portion of a number.
// Use this func if you are sure "val" evaluates to a number.
// More reliable than: return val-Math.floor(val), which should
// return the decimal portion, but doesn't always.
function getDecimal(val){
val+="";
return val.substring(val.indexOf("."));
}
// Use commaThis() function to convert a numerical expression
// to a number with commas(a String), e.g. 10000.25 to "10,000.25"
// Use this func if you are sure "entry" evaluates to a number.
function commaThis(entry) {
entry=Math.floor(entry)+""
var rex = /(-?\d+)(\d{3})/
while (rex.test(entry)) {
entry = entry.replace(rex, "$1,$2")
}
return entry+getDecimal(entry);
}
Mick
Josh Austin
System Administrator
Agent Services of America
ksadmin NOSPAM @comcast.net
(remove NOSPAM and the spaces out of my email address to send email)
Josh Austin
System Administrator
Agent Services of America
ksadmin NOSPAM @comcast.net
(remove NOSPAM and the spaces out of my email address to send email)