Wrong syntax for formatNumber() ? I'm stuck.

G

Greg

I have a function for totalling decimal values provided in textboxes
that form a column on a webpage. This script should total the values
in the textboxes, displaying the result in the final
'optotalprogtime' text box formatted as a decimal.

Here is what I've got so far, but I'm getting annoying "object
expected" errors on the line where I attempt to format the number (as
indicated by the comment below)


//realtime calculation formating as minutes (decimals)
function opprogtotalcol() {
alert("in opprogtotalcol");
document.getElementById('optotalprogtime').value = 0;
for (var rowNumber =1; rowNumber <= 12; rowNumber++){
document.getElementById('optotalprogtime').value =
parseFloat(document.getElementById('optotalprogtime').value) +
parseFloat(document.getElementById('opprogmin' + rowNumber ).value);
}
if(!isNaN(document.getElementById('optotalprogtime').value)){
//only works for numbers
document.getElementById('optotalprogtime').value =
formatNumber(document.getElementById('optotalprogtime').value,
2);//format number here
}
}

Any insight would be appreciated.

To head off concerns about wrapping code correctly, my code is
wrapping pretty bad in this form, but in the acutal page I have long
lines that do not wrap.

Thanks in advance.

Greg McKone
 
G

Greg

SORRY, got confused with VBSCRIPT's FormatNumber() ouch.

Now I'm using .toFixed(2)

can anyone see a problem with doing the following?

document.getElementById('optotalcyclemin').value =
document.getElementById('optotalcyclemin').value.toFixed(2);//format number

Thanks.
 
G

Greg

This works now that I've changed it to read.

document.getElementById('optotalcyclemin').value =
parseFloat(document.getElementById('optotalcyclemin').value).toFixed(2);//format
number

the value was supposed to be numeric, so I suppose that writing it to a
text box forces it to be a string. I'll try to remember that.

Greg. (again.)
 
D

Dr John Stockton

seen in said:
SORRY, got confused with VBSCRIPT's FormatNumber() ouch.

Now I'm using .toFixed(2)

can anyone see a problem with doing the following?

document.getElementById('optotalcyclemin').value =
document.getElementById('optotalcyclemin').value.toFixed(2);//format number

Thanks.

Responses should go after trimmed quotes; see newsgroup FAQ.

Function toFixed() is not available in all browsers; see FAQ.

With long lines in your original, you should extend your
margins so that YOU do not wrap them and WE have the choice.

"with", sensibly used, is a great help;

document.getElementById('optotalprogtime').value = formatNumber(document.getElementById('optotalprogtime').value,

can be written

with (document.getElementById('optotalprogtime')) value = formatNumber(value, 2)

And
document.getElementById('optotalprogtime').value = parseFloat(document.getElementById('optotalprogtime').value) + parseFloat(document.getElementById('opprogmin' + rowNumber ).value);

is better as
T += parseFloat(document.getElementById('opprogmin' + rowNumber ).value);
with one assignment to whatever-it-was.

Unary + can be used instead of parseFloat; RTF.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top