format currency function has problems

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

    format currency function has problems

    I found the following script somewhere so I can change values to money before
    the form is printed. The page works for most people but not some. It worked
    fine for me but I get the yellow tri-angle with an exclamation point in the
    bottom left part of the window. When I told my IE to display a notification
    about every error...it told me there was a problem with line 11.
    What is wrong with this?

    9 function formatCurrency( strValue)
    10 {
    11 strValue = strValue.toStri ng().replace(/\$|\,/g,'');
    12 dblValue = parseFloat(strV alue);
    13 blnSign = (dblValue == (dblValue = Math.abs(dblVal ue)));
    14 dblValue = Math.floor(dblV alue*100+0.5000 0000001);
    15 intCents = dblValue%100;
    16 strCents = intCents.toStri ng();
    17 dblValue = Math.floor(dblV alue/100).toString() ;
    18 if(intCents<10)
    19 strCents = "0" + strCents;
    20 for (var i = 0; i < Math.floor((dbl Value.length-(1+i))/3); i++)
    21 dblValue = dblValue.substr ing(0,dblValue. length-(4*i+3))+','+
    22 dblValue.substr ing(dblValue.le ngth-(4*i+3));
    23 return (((blnSign)?'': '-') + '$' + dblValue + '.' + strCents);
    }

    error message
    Line 11
    Char 2
    Error 'undefined' is null or not an object
    code 0
  • Michael Winter

    #2
    Re: format currency function has problems

    On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <abbylee26@hotm ail.com> wrote:
    [color=blue]
    > I found the following script somewhere [...][/color]

    I seriously suggest you put it back.
    [color=blue]
    > When I told my IE to display a notification about every error [...][/color]

    You don't need to do that. You could just double-click on the icon on the
    status bar.
    [color=blue]
    > What is wrong with this?[/color]

    Aside from it being an unreadable mess, and containing some dubious hacks?

    Try:

    /* n - Number to format (in pennies).
    * c - Currency symbol to use (defaults to none).
    * g - Grouping symbol (defaults to none).
    *
    * Outputs a number of the form cngnnngnnn.nn
    *
    * For example, toCurrency(1426 35.7, '£', ',') produces
    * £1,426.36
    */
    function toCurrency(n, c, g) {
    var s = (0 > n) ? '-' : ''; n = Math.abs(n);
    var m = String(Math.rou nd(n));
    var j, i = '', f; c = c || ''; g = g || '';

    while(m.length < 3) {m = '0' + m;}
    f = m.substring((j = m.length - 2));
    while(j > 3) {
    i = g + m.substring(j - 3, j) + i;
    j -= 3;
    }
    i = m.substring(0, j) + i;
    return s + c + i + '.' + f;
    }

    Note the instructions for the first parameter: the value should be in
    pennies (cents). Currency-related calculations should be performed like
    this anyway to reduce the change of errors. If you aren't using pennies
    and you don't want to change your existing script, multiply by 100 when
    passing the value.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Abby Lee

      #3
      Re: format currency function has problems

      "Michael Winter" <M.Winter@bluey onder.co.invali d> writes:
      [color=blue]
      > On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <abbylee26@hotm ail.com> wrote:
      >[color=green]
      > > I found the following script somewhere [...][/color]
      >
      > I seriously suggest you put it back.
      >[/color]

      Now that's funny!
      thanks for your help.

      Comment

      • Dr John Stockton

        #4
        Re: format currency function has problems

        JRS: In article <opsf4o6zuox13k vk@atlantis>, dated Tue, 19 Oct 2004
        17:49:22, seen in news:comp.lang. javascript, Michael Winter <M.Winter@bl
        ueyonder.co.inv alid> posted :[color=blue]
        >On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <abbylee26@hotm ail.com> wrote:
        >[color=green]
        >> I found the following script somewhere [...][/color]
        >
        >I seriously suggest you put it back.[/color]

        Pereant, inquit, qui ante nos nostra dixerunt.

        [color=blue]
        >Try:
        >
        > /* n - Number to format (in pennies).
        > * c - Currency symbol to use (defaults to none).
        > * g - Grouping symbol (defaults to none).[/color]

        or Thousands separator

        ISTM that it should also have
        * d - Decimal separator (defaults to '.').
        [color=blue]
        > ...[/color]
        [color=blue]
        >Note the instructions for the first parameter: the value should be in
        >pennies (cents).
        > ...[/color]

        Bring back the ha'penny and farthing!

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...