Formatting the results of a calculation

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)
 
M

Mick White

Lasse Reichstein Nielsen wrote:

---
function formatNumber(n, maxDecimals) {
maxDecimals = maxDecimals || 16;
// make sure it is a number
n = Number(n);
if (isNaN(n)) { return n; }
// sign
var neg = n < 0;
if (neg) { n = -n; }
// integral part
var intPart = Math.floor(n);
// fractional part
var fracPart = n - intPart;


var intPart = Math.floor(n);
// fractional part
var fracPart = n - intPart;

The calculation above is problematic, consider:

alert(formatNumber(-10000.555666777,12))

The decimal part is better retrieved by string manipulation. IMVHO.

Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
I have a question which has had me stumped for a few days now. I have
a form that I add the values of fields together and display the total
in a total field. I have the calculations working correctly. My
problem is that 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.

Thanks in advance.

function calculateTotal(){
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);
}
total=val0+val1+val2+val3+val4-val5
document.frmLifeNeed.total.value = total;
}


Firstly, read the regularly-posted FAQ, and the FAQ notes, carefully on
the subject of newsgroup article formatting; in particular, line
wrapping, quoting, signature separators. And, when you can, the new
parts of the next FAQ issue.

Never use parseInt without a second parameter unless you understand and
require what it may do. For example, what do you expect the result of
parseInt('0042e5') to be? To convert a string value into a number, see
the corresponding FAQ entry; I would use a unary + but others prefer
Number().

Never use eval, except to evaluate an externally-supplied string.

Your routine above is weird; I'm sure it could be done better, but to
make a detailed recommendation would require me to understand your
actual intent.

For the subsequent insertion of commas, see
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Out>

An Administrator should be employing programmers, not attempting to
emulate one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top