Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Wrong syntax for formatNumber() ? I'm stuck.

Reply
Thread Tools

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

 
 
Greg
Guest
Posts: n/a
 
      09-13-2003
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('optotalprogtim e').value) +
parseFloat(document.getElementById('opprogmin' + rowNumber ).value);
}
if(!isNaN(document.getElementById('optotalprogtime ').value)){
//only works for numbers
document.getElementById('optotalprogtime').value =
formatNumber(document.getElementById('optotalprogt ime').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
 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      09-14-2003
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.t oFixed(2);//format number

Thanks.

Greg wrote:
> 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('optotalprogtim e').value) +
> parseFloat(document.getElementById('opprogmin' + rowNumber ).value);
> }
> if(!isNaN(document.getElementById('optotalprogtime ').value)){
> //only works for numbers
> document.getElementById('optotalprogtime').value =
> formatNumber(document.getElementById('optotalprogt ime').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


 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      09-14-2003
This works now that I've changed it to read.

document.getElementById('optotalcyclemin').value =
parseFloat(document.getElementById('optotalcyclemi n').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.)

Greg wrote:
> 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.t oFixed(2);//format number
>
> Thanks.
>
> Greg wrote:
>
>> 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('optotalprogtim e').value) +
>> parseFloat(document.getElementById('opprogmin' + rowNumber ).value);
>> }
>> if(!isNaN(document.getElementById('optotalprogtime ').value)){
>> //only works for numbers
>> document.getElementById('optotalprogtime').value =
>> formatNumber(document.getElementById('optotalprogt ime').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

>
>


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-14-2003
JRS: In article <oIP8b.3250$Cu3.3147@edtnps84>, seen in
news:comp.lang.javascript, Greg <(E-Mail Removed)> posted at Sun, 14 Sep 2003 01:42:44 :-
>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').valu e =
>document.getElementById('optotalcyclemin').value. toFixed(2);//format number
>
>Thanks.
>
>Greg wrote:
>> 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.


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('optotalprogt ime').value,

can be written

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

And
document.getElementById('optotalprogtime').value = parseFloat(document.getElementById('optotalprogtim e').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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments