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

Discussion in 'Javascript' started by Greg, Sep 13, 2003.

  1. Greg

    Greg Guest

    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
    Greg, Sep 13, 2003
    #1
    1. Advertising

  2. Greg

    Greg Guest

    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.

    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('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
    Greg, Sep 14, 2003
    #2
    1. Advertising

  3. Greg

    Greg Guest

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

    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.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.
    >>
    >> 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

    >
    >
    Greg, Sep 14, 2003
    #3
  4. JRS: In article <oIP8b.3250$Cu3.3147@edtnps84>, seen in
    news:comp.lang.javascript, Greg <> 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').value =
    >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('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.

    --
    © 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.
    Dr John Stockton, Sep 14, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff
    Replies:
    10
    Views:
    489
    Jim Langston
    Jun 23, 2007
  2. Replies:
    10
    Views:
    530
    Jerry Coffin
    May 26, 2008
  3. eddie wang

    formatnumber function returns error

    eddie wang, Feb 10, 2004, in forum: ASP General
    Replies:
    11
    Views:
    511
    Bob Barrows [MVP]
    Feb 11, 2004
  4. Lakrom

    Error with FormatNumber sentence

    Lakrom, Dec 16, 2004, in forum: ASP General
    Replies:
    4
    Views:
    132
    Lakrom
    Dec 16, 2004
  5. MiniEggs

    FormatNumber

    MiniEggs, Dec 4, 2009, in forum: ASP General
    Replies:
    4
    Views:
    918
    Bob Barrows
    Dec 4, 2009
Loading...

Share This Page