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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg

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

    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
    'optotalprogtim e' 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.getEle mentById('optot alprogtime').va lue = 0;
    for (var rowNumber =1; rowNumber <= 12; rowNumber++){
    document.getEle mentById('optot alprogtime').va lue =
    parseFloat(docu ment.getElement ById('optotalpr ogtime').value) +
    parseFloat(docu ment.getElement ById('opprogmin ' + rowNumber ).value);
    }
    if(!isNaN(docum ent.getElementB yId('optotalpro gtime').value)) {
    //only works for numbers
    document.getEle mentById('optot alprogtime').va lue =
    formatNumber(do cument.getEleme ntById('optotal progtime').valu e,
    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

    #2
    Re: Wrong syntax for formatNumber() ? I'm stuck.

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

    Now I'm using .toFixed(2)

    can anyone see a problem with doing the following?

    document.getEle mentById('optot alcyclemin').va lue =
    document.getEle mentById('optot alcyclemin').va lue.toFixed(2);//format number

    Thanks.

    Greg wrote:[color=blue]
    > 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
    > 'optotalprogtim e' 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.getEle mentById('optot alprogtime').va lue = 0;
    > for (var rowNumber =1; rowNumber <= 12; rowNumber++){
    > document.getEle mentById('optot alprogtime').va lue =
    > parseFloat(docu ment.getElement ById('optotalpr ogtime').value) +
    > parseFloat(docu ment.getElement ById('opprogmin ' + rowNumber ).value);
    > }
    > if(!isNaN(docum ent.getElementB yId('optotalpro gtime').value)) {
    > //only works for numbers
    > document.getEle mentById('optot alprogtime').va lue =
    > formatNumber(do cument.getEleme ntById('optotal progtime').valu e,
    > 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[/color]

    Comment

    • Greg

      #3
      Re: Wrong syntax for formatNumber() ? I'm stuck.

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

      document.getEle mentById('optot alcyclemin').va lue =
      parseFloat(docu ment.getElement ById('optotalcy clemin').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:[color=blue]
      > SORRY, got confused with VBSCRIPT's FormatNumber() ouch.
      >
      > Now I'm using .toFixed(2)
      >
      > can anyone see a problem with doing the following?
      >
      > document.getEle mentById('optot alcyclemin').va lue =
      > document.getEle mentById('optot alcyclemin').va lue.toFixed(2);//format number
      >
      > Thanks.
      >
      > Greg wrote:
      >[color=green]
      >> 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 'optotalprogtim e'
      >> 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.getEle mentById('optot alprogtime').va lue = 0;
      >> for (var rowNumber =1; rowNumber <= 12; rowNumber++){
      >> document.getEle mentById('optot alprogtime').va lue =
      >> parseFloat(docu ment.getElement ById('optotalpr ogtime').value) +
      >> parseFloat(docu ment.getElement ById('opprogmin ' + rowNumber ).value);
      >> }
      >> if(!isNaN(docum ent.getElementB yId('optotalpro gtime').value)) {
      >> //only works for numbers
      >> document.getEle mentById('optot alprogtime').va lue =
      >> formatNumber(do cument.getEleme ntById('optotal progtime').valu e,
      >> 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[/color]
      >
      >[/color]

      Comment

      • Dr John Stockton

        #4
        Re: Wrong syntax for formatNumber() ? I'm stuck.

        JRS: In article <oIP8b.3250$Cu3 .3147@edtnps84> , seen in
        news:comp.lang. javascript, Greg <junkmail@green treesoftwaresys tems.com> posted at Sun, 14 Sep 2003 01:42:44 :-[color=blue]
        >SORRY, got confused with VBSCRIPT's FormatNumber() ouch.
        >
        >Now I'm using .toFixed(2)
        >
        >can anyone see a problem with doing the following?
        >
        >document.getEl ementById('opto talcyclemin').v alue =
        >document.getEl ementById('opto talcyclemin').v alue.toFixed(2) ;//format number
        >
        >Thanks.
        >
        >Greg wrote:[color=green]
        >> 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
        >> 'optotalprogtim e' text box formatted as a decimal.[/color][/color]

        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.getEle mentById('optot alprogtime').va lue = formatNumber(do cument.getEleme ntById('optotal progtime').valu e,

        can be written

        with (document.getEl ementById('opto talprogtime')) value = formatNumber(va lue, 2)

        And
        document.getEle mentById('optot alprogtime').va lue = parseFloat(docu ment.getElement ById('optotalpr ogtime').value) + parseFloat(docu ment.getElement ById('opprogmin ' + rowNumber ).value);

        is better as
        T += parseFloat(docu ment.getElement ById('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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...