Formatting Number With Thousands Separator

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

    Formatting Number With Thousands Separator

    Gday,


    How would I format a number so that:

    TheValue = 32500

    Displays in the TextBox as: $32,500.00



    Thanks a lot :)
    -Douglas


  • Randy Webb

    #2
    Re: Formatting Number With Thousands Separator

    Douglas wrote:
    [color=blue]
    > Gday,
    >
    >
    > How would I format a number so that:
    >
    > TheValue = 32500
    >
    > Displays in the TextBox as: $32,500.00
    >[/color]

    And if the person viewing the page uses . as a separator instead of ,
    and uses , instead of . for the decimal?

    But, for a hint, convert your number to a String, reverse the String,
    then insert the separators, then reverse it back. And then read the FAQ
    to find out how to add the 00 on the end (Its in there).

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • Dr John Stockton

      #3
      Re: Formatting Number With Thousands Separator

      JRS: In article <da-dnSh_pfanrendRV n-jg@comcast.com> , seen in
      news:comp.lang. javascript, Randy Webb <hikksnotathome @aol.com> posted at
      Wed, 7 Apr 2004 12:45:08 :[color=blue]
      >Douglas wrote:[/color]
      [color=blue][color=green]
      >> How would I format a number so that:
      >>
      >> TheValue = 32500
      >>
      >> Displays in the TextBox as: $32,500.00
      >>[/color]
      >
      >And if the person viewing the page uses . as a separator instead of ,
      >and uses , instead of . for the decimal?[/color]

      Does any form of javascript recognise the possibility of a different
      decimal separator or the possibility of a thousands separator (except by
      added code, of course)?

      If so, converting the number 12345.6 to string will allow then to be
      determined.

      [color=blue]
      >But, for a hint, convert your number to a String, reverse the String,
      >then insert the separators, then reverse it back. And then read the FAQ
      >to find out how to add the 00 on the end (Its in there).[/color]

      There's no need to reverse the string.
      <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#OutCo mma>

      --
      © 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> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Formatting Number With Thousands Separator

        Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
        [color=blue]
        > Does any form of javascript recognise the possibility of a different
        > decimal separator or the possibility of a thousands separator (except by
        > added code, of course)?[/color]

        I can't say that there isn't some application of ECMAScript that doesn't
        contain utility functions, but it's not part of ECMAScript, and it's not
        in the usual clients.
        [color=blue]
        > There's no need to reverse the string.
        > <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#OutCo mma>[/color]

        I notice the result -,123 :)

        If one only cares about recent versions of Javascript, regular
        expressions with lokahead can be used to make it easier:
        ---
        function tsep(n,swap) {
        var ts=",", ds="."; // thousands and decimal separators
        if (swap) { ts=","; ts="."; } // swap if requested

        var ns = String(n),ps=ns ,ss=""; // numString, prefixString, suffixString
        var i = ns.indexOf(".") ;
        if (i!=-1) { // if ".", then split:
        ps = ns.substring(0, i);
        ss = ds+ns.substring (i+1);
        }
        return ps.replace(/(\d)(?=(\d{3})+ ([.]|$))/g,"$1"+ts)+ss ;
        }
        ---

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Dennis M. Marks

          #5
          Re: Formatting Number With Thousands Separator

          In article <4074266f$0$276 43$61ce578d@new s.syd.swiftdsl. com.au>,
          Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:
          [color=blue]
          > Gday,
          >
          >
          > How would I format a number so that:
          >
          > TheValue = 32500
          >
          > Displays in the TextBox as: $32,500.00
          >
          >
          >
          > Thanks a lot :)
          > -Douglas
          >
          >[/color]
          I took the challenge and wrote the following. If you find an error, let
          me know.

          <script type="text/javascript">

          /// This script will format positive money values. Pass it a number
          with or without decimal digits. It will be formatted with the currency,
          thousands, and decimal symbols passed to it.
          /// PASSED PARAMETERS
          /// theNumber - the number to be formatted
          /// theCurrency - the currency symbol
          /// theThousands - the thousands separator
          /// theDecimal - the decimal separator

          function isThousands(pos ition) {
          if (Math.floor(pos ition/3)*3==position) return true;
          return false;
          };

          function formatMoney (theNumber,theC urrency,theThou sands,theDecima l) {
          var theDecimalDigit s =
          Math.round((the Number*100)-(Math.floor(the Number)*100));
          theDecimalDigit s= ""+ (theDecimalDigi ts + "0").substring( 0,2);
          theNumber = ""+Math.floor(t heNumber);
          var theOutput = theCurrency;
          for (x=0; x<theNumber.len gth; x++) {
          theOutput += theNumber.subst ring(x,x+1);
          if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1
          !=0)) {
          theOutput += theThousands;
          };
          };
          theOutput += theDecimal + theDecimalDigit s;
          return theOutput;
          };


          alert(formatMon ey(12345.67,"$" ,",","."))
          alert(formatMon ey(123456,"£"," .",","))

          </script>

          --
          Dennis M. Marks

          Replace domain.invalid with dcsi.net


          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

          Comment

          • Douglas

            #6
            Re: Formatting Number With Thousands Separator

            Gday Dennis :)

            thanks very much for your efforts...

            While I'm not new to programming, this is probably my first attempt to
            modify my own JavaScript.

            I have made a couple of minor additions to the code you supplied, and
            honestly, didnt have the faintest idea where to start because the syntax /
            object model / typing etc are so vastly different from the code I am used to
            writing.

            Here 'tis... prolly a bit longhand and I'm sure theres more 'efficient' ways
            to do the same thing but lets face it its prolly only a couple of cpu cycles
            and additionally, those cpu cycles are not run on /my/ server :))

            Thanks Again.
            -Douglas


            /// ///
            /// JavaScript FormatCurrency Function - FormatCurrency. js ///
            /// ///

            /// This script will format positive money values. Pass it a number
            /// with or without decimal digits. It will be formatted with the currency,
            /// thousands, and decimal symbols passed to it.

            /// PASSED PARAMETERS
            /// theNumber - the number to be formatted
            /// theCurrency - the currency symbol
            /// theThousands - the thousands separator
            /// theDecimal - the decimal separator

            function isThousands(pos ition)
            {
            if (Math.floor(pos ition/3)*3==position) return true;
            return false;
            };


            function formatMoney (theNumber,theC urrency,theThou sands,theDecima l)
            {

            if (theDecimal==un defined)
            {
            var theDecimalDigit s = "";

            } else {

            var theDecimalDigit s =
            Math.round((the Number*100)-(Math.floor(the Number)*100));
            }

            theDecimalDigit s= "" + (theDecimalDigi ts + "0").substring( 0,2);

            theNumber = "" + Math.floor(theN umber);

            var theOutput = theCurrency;

            for (x=0; x<theNumber.len gth; x++)
            {

            theOutput += theNumber.subst ring(x, x+1);

            if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1!=0))
            {
            theOutput += theThousands;
            };
            };

            if (theDecimal!=un defined)
            {
            theOutput += theDecimal + theDecimalDigit s;
            }

            return theOutput;
            };












            ----- Original Message -----
            From: "Dennis M. Marks" <denmarks@domai n.invalid>
            Newsgroups: comp.lang.javas cript
            Sent: Thursday, April 08, 2004 9:03 AM
            Subject: Re: Formatting Number With Thousands Separator

            [color=blue]
            > In article <4074266f$0$276 43$61ce578d@new s.syd.swiftdsl. com.au>,
            > Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:
            >[color=green]
            > > Gday,
            > >
            > >
            > > How would I format a number so that:
            > >
            > > TheValue = 32500
            > >
            > > Displays in the TextBox as: $32,500.00
            > >
            > >
            > >
            > > Thanks a lot :)
            > > -Douglas
            > >
            > >[/color]
            > I took the challenge and wrote the following. If you find an error, let
            > me know.
            >
            > <script type="text/javascript">
            >
            > /// This script will format positive money values. Pass it a number
            > with or without decimal digits. It will be formatted with the currency,
            > thousands, and decimal symbols passed to it.
            > /// PASSED PARAMETERS
            > /// theNumber - the number to be formatted
            > /// theCurrency - the currency symbol
            > /// theThousands - the thousands separator
            > /// theDecimal - the decimal separator
            >
            > function isThousands(pos ition) {
            > if (Math.floor(pos ition/3)*3==position) return true;
            > return false;
            > };
            >
            > function formatMoney (theNumber,theC urrency,theThou sands,theDecima l) {
            > var theDecimalDigit s =
            > Math.round((the Number*100)-(Math.floor(the Number)*100));
            > theDecimalDigit s= ""+ (theDecimalDigi ts + "0").substring( 0,2);
            > theNumber = ""+Math.floor(t heNumber);
            > var theOutput = theCurrency;
            > for (x=0; x<theNumber.len gth; x++) {
            > theOutput += theNumber.subst ring(x,x+1);
            > if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1
            > !=0)) {
            > theOutput += theThousands;
            > };
            > };
            > theOutput += theDecimal + theDecimalDigit s;
            > return theOutput;
            > };
            >
            >
            > alert(formatMon ey(12345.67,"$" ,",","."))
            > alert(formatMon ey(123456,"£"," .",","))
            >
            > </script>
            >
            > --
            > Dennis M. Marks
            > http://www.dcs-chico.com/~denmarks/
            > Replace domain.invalid with dcsi.net
            >
            >
            > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
            > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
            > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


            Comment

            • Dr John Stockton

              #7
              Re: Formatting Number With Thousands Separator

              JRS: In article <3c7fe3gh.fsf@h otpop.com>, seen in
              news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
              posted at Thu, 8 Apr 2004 02:08:30 :[color=blue]
              >[color=green]
              >> There's no need to reverse the string.
              >> <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#OutCo mma>[/color]
              >
              >I notice the result -,123 :)[/color]

              In a section which starts "Note that ThouS is for non-negative integers,
              and Comma is for integers.", from function ThouS, the first line of
              which was

              function ThouS(SS) { var X = "", S = String(SS), L // SS >= 0

              so that result demonstrates that the comment is significant.

              That line of the results is now marked as comment.

              --
              © 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> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • Dr John Stockton

                #8
                Re: Formatting Number With Thousands Separator

                JRS: In article <07042004180345 6176%denmarks@d omain.invalid>, seen in
                news:comp.lang. javascript, Dennis M. Marks <denmarks@domai n.invalid>
                posted at Wed, 7 Apr 2004 18:03:45 :
                [color=blue]
                >I took the challenge and wrote the following. If you find an error, let
                >me know.[/color]
                [color=blue]
                >/// This script will format positive money values. Pass it a number[/color]

                /* !!! */ alert(formatMon ey(0.07, "£", ".", ",")) // -> 0.70


                FAQ 4.6, IIRC, used that underlying method up to 2001-01-22. It was
                replaced. The FAQ section cites a page which includes a section
                "Testing" which recommends numbers to try : St Luke: Chapter 10, Verse
                37, tail, applies.

                Your method degrades badly at sums of $1e21 and up, too.

                BTW, the X-Comments line of your header is defective.

                Familiarising oneself with a newsgroup FAQ before posting protects
                against unnecessary embarrassment.

                --
                © 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> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • rh

                  #9
                  Re: Formatting Number With Thousands Separator

                  Lasse Reichstein Nielsen wrote:
                  <snip>[color=blue]
                  > If one only cares about recent versions of Javascript, regular
                  > expressions with lokahead can be used to make it easier:
                  > ---
                  > function tsep(n,swap) {
                  > var ts=",", ds="."; // thousands and decimal separators
                  > if (swap) { ts=","; ts="."; } // swap if requested
                  >
                  > var ns = String(n),ps=ns ,ss=""; // numString, prefixString, suffixString
                  > var i = ns.indexOf(".") ;
                  > if (i!=-1) { // if ".", then split:
                  > ps = ns.substring(0, i);
                  > ss = ds+ns.substring (i+1);
                  > }
                  > return ps.replace(/(\d)(?=(\d{3})+ ([.]|$))/g,"$1"+ts)+ss ;
                  > }
                  > ---
                  >
                  > /L[/color]

                  An alternative that doesn't rely on lookahead, and includes
                  (truncated) $ formatting requested by the OP:

                  function tsep(n,swap) {
                  var ns = String(n), seps = [".",","];
                  if(swap) seps.reverse();
                  while(/^([^.,]*\d)(\d{3}([.,]|$))/.test(ns)) {
                  ns = ns.replace(/^([^.,]*\d)(\d{3}([.,]|$))/,"$1"+seps[1]+"$2");
                  }
                  ns += ((ns.indexOf(se ps[0]) < 0) ? seps[0] : "") +"00";
                  return "$"+ns.repl ace(/(\d{2})\d*$/i,"$1");
                  }

                  Note that e-formatted n is not checked/handled.

                  ../rh

                  Comment

                  • Dennis M. Marks

                    #10
                    Re: Formatting Number With Thousands Separator

                    In article <40757a0c$0$276 47$61ce578d@new s.syd.swiftdsl. com.au>,
                    Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:
                    [color=blue]
                    > Gday Dennis :)
                    >
                    > thanks very much for your efforts...
                    >
                    > While I'm not new to programming, this is probably my first attempt to
                    > modify my own JavaScript.
                    >
                    > I have made a couple of minor additions to the code you supplied, and
                    > honestly, didnt have the faintest idea where to start because the syntax /
                    > object model / typing etc are so vastly different from the code I am used to
                    > writing.
                    >
                    > Here 'tis... prolly a bit longhand and I'm sure theres more 'efficient' ways
                    > to do the same thing but lets face it its prolly only a couple of cpu cycles
                    > and additionally, those cpu cycles are not run on /my/ server :))
                    >
                    > Thanks Again.
                    > -Douglas
                    >
                    >
                    > /// ///
                    > /// JavaScript FormatCurrency Function - FormatCurrency. js ///
                    > /// ///
                    >
                    > /// This script will format positive money values. Pass it a number
                    > /// with or without decimal digits. It will be formatted with the currency,
                    > /// thousands, and decimal symbols passed to it.
                    >
                    > /// PASSED PARAMETERS
                    > /// theNumber - the number to be formatted
                    > /// theCurrency - the currency symbol
                    > /// theThousands - the thousands separator
                    > /// theDecimal - the decimal separator
                    >
                    > function isThousands(pos ition)
                    > {
                    > if (Math.floor(pos ition/3)*3==position) return true;
                    > return false;
                    > };
                    >
                    >
                    > function formatMoney (theNumber,theC urrency,theThou sands,theDecima l)
                    > {
                    >
                    > if (theDecimal==un defined)
                    > {
                    > var theDecimalDigit s = "";
                    >
                    > } else {
                    >
                    > var theDecimalDigit s =
                    > Math.round((the Number*100)-(Math.floor(the Number)*100));
                    > }
                    >
                    > theDecimalDigit s= "" + (theDecimalDigi ts + "0").substring( 0,2);
                    >
                    > theNumber = "" + Math.floor(theN umber);
                    >
                    > var theOutput = theCurrency;
                    >
                    > for (x=0; x<theNumber.len gth; x++)
                    > {
                    >
                    > theOutput += theNumber.subst ring(x, x+1);
                    >
                    > if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1!=0))
                    > {
                    > theOutput += theThousands;
                    > };
                    > };
                    >
                    > if (theDecimal!=un defined)
                    > {
                    > theOutput += theDecimal + theDecimalDigit s;
                    > }
                    >
                    > return theOutput;
                    > };
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    > ----- Original Message -----
                    > From: "Dennis M. Marks" <denmarks@domai n.invalid>
                    > Newsgroups: comp.lang.javas cript
                    > Sent: Thursday, April 08, 2004 9:03 AM
                    > Subject: Re: Formatting Number With Thousands Separator
                    >
                    >[color=green]
                    > > In article <4074266f$0$276 43$61ce578d@new s.syd.swiftdsl. com.au>,
                    > > Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:
                    > >[color=darkred]
                    > > > Gday,
                    > > >
                    > > >
                    > > > How would I format a number so that:
                    > > >
                    > > > TheValue = 32500
                    > > >
                    > > > Displays in the TextBox as: $32,500.00
                    > > >
                    > > >
                    > > >
                    > > > Thanks a lot :)
                    > > > -Douglas
                    > > >
                    > > >[/color]
                    > > I took the challenge and wrote the following. If you find an error, let
                    > > me know.
                    > >
                    > > <script type="text/javascript">
                    > >
                    > > /// This script will format positive money values. Pass it a number
                    > > with or without decimal digits. It will be formatted with the currency,
                    > > thousands, and decimal symbols passed to it.
                    > > /// PASSED PARAMETERS
                    > > /// theNumber - the number to be formatted
                    > > /// theCurrency - the currency symbol
                    > > /// theThousands - the thousands separator
                    > > /// theDecimal - the decimal separator
                    > >
                    > > function isThousands(pos ition) {
                    > > if (Math.floor(pos ition/3)*3==position) return true;
                    > > return false;
                    > > };
                    > >
                    > > function formatMoney (theNumber,theC urrency,theThou sands,theDecima l) {
                    > > var theDecimalDigit s =
                    > > Math.round((the Number*100)-(Math.floor(the Number)*100));
                    > > theDecimalDigit s= ""+ (theDecimalDigi ts + "0").substring( 0,2);
                    > > theNumber = ""+Math.floor(t heNumber);
                    > > var theOutput = theCurrency;
                    > > for (x=0; x<theNumber.len gth; x++) {
                    > > theOutput += theNumber.subst ring(x,x+1);
                    > > if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1
                    > > !=0)) {
                    > > theOutput += theThousands;
                    > > };
                    > > };
                    > > theOutput += theDecimal + theDecimalDigit s;
                    > > return theOutput;
                    > > };
                    > >
                    > >
                    > > alert(formatMon ey(12345.67,"$" ,",","."))
                    > > alert(formatMon ey(123456,"£"," .",","))
                    > >
                    > > </script>
                    > >
                    > > --
                    > > Dennis M. Marks
                    > > http://www.dcs-chico.com/~denmarks/
                    > > Replace domain.invalid with dcsi.net
                    > >
                    > >
                    > > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                    > > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                    > > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]
                    >
                    >[/color]
                    There is a bug that I am checking into. If the first digit after the
                    decimal is zero it drops it.

                    --
                    Dennis M. Marks

                    Replace domain.invalid with dcsi.net


                    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                    Comment

                    • Dennis M. Marks

                      #11
                      Re: Formatting Number With Thousands Separator - corrected

                      Corrected Script
                      if (theDecimalDigi ts < 10) {theDecimalDigi ts = "0"+theDecimalD igits};
                      ADDED


                      <script type="text/javascript">

                      /// This script will format positive money values. Pass it a number
                      with or without decimal digits. It will be formatted with the currency,
                      thousands, and decimal symbols passed to it.
                      /// PASSED PARAMETERS
                      /// theNumber - the number to be formatted
                      /// theCurrency - the currency symbol
                      /// theThousands - the thousands separator
                      /// theDecimal - the decimal separator

                      function isThousands(pos ition) {
                      if (Math.floor(pos ition/3)*3==position) return true;
                      return false;
                      };

                      function formatMoney (theNumber,theC urrency,theThou sands,theDecima l) {
                      var theDecimalDigit s =
                      Math.round((the Number*100)-(Math.floor(the Number)*100));
                      if (theDecimalDigi ts < 10) {theDecimalDigi ts = "0"+theDecimalD igits};
                      theDecimalDigit s= ""+ (theDecimalDigi ts + "0").substring( 0,2);
                      theNumber = ""+Math.floor(t heNumber);
                      var theOutput = theCurrency;
                      for (x=0; x<theNumber.len gth; x++) {
                      theOutput += theNumber.subst ring(x,x+1);
                      if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1 !=
                      0)) {
                      theOutput += theThousands;
                      };
                      };
                      theOutput += theDecimal + theDecimalDigit s;
                      return theOutput;
                      };

                      </script>

                      --
                      Dennis M. Marks

                      Replace domain.invalid with dcsi.net


                      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                      Comment

                      • Douglas

                        #12
                        Re: Formatting Number With Thousands Separator - corrected

                        ok cool

                        didnt actually notice that problem but thanks for the bugfix :)

                        what i was trying to do with my addition was (in pseudocode):

                        if no decimal parameter is passed,
                        dont display the decimal value
                        (i suppose it /should/ round at this point? =P)
                        otherwise
                        go the whole hog
                        end if

                        sometimes i need to call the full formula

                        ie:
                        cost per m² = $13.65

                        but the end result will usually be something like:

                        overall cost = cost per m² * area = $136,500

                        so in that case i dont want to include the decimal.



                        on a second issue, consider the following:

                        --------------------------------
                        area cost/m² total
                        --------------------------------
                        area1 100 $13.65 $1,365
                        area2 1000 $14.52 $14,520
                        area3 10000 $17.49 $174,900
                        --------------------------------
                        grand total: $190,785
                        --------------------------------

                        the grand total now comes up with NaN (not a number i assume?)

                        even though the formula reads:

                        gtotal = parseFloat(area 1) + parseFloat(area 2) + parseFloat(area 3);

                        not sure if i should be using parseFloat or ParseInt...

                        as I mentioned i dont have a c/jscript background so some of this is a
                        little unclear to me.



                        thanks again for your help :)
                        -Douglas




                        ----- Original Message -----
                        From: "Dennis M. Marks" <denmarks@domai n.invalid>
                        Newsgroups: comp.lang.javas cript
                        Sent: Friday, April 09, 2004 6:02 AM
                        Subject: Re: Formatting Number With Thousands Separator - corrected

                        [color=blue]
                        > Corrected Script
                        > if (theDecimalDigi ts < 10) {theDecimalDigi ts = "0"+theDecimalD igits};
                        > ADDED
                        >
                        >
                        > <script type="text/javascript">
                        >
                        > /// This script will format positive money values. Pass it a number
                        > with or without decimal digits. It will be formatted with the currency,
                        > thousands, and decimal symbols passed to it.
                        > /// PASSED PARAMETERS
                        > /// theNumber - the number to be formatted
                        > /// theCurrency - the currency symbol
                        > /// theThousands - the thousands separator
                        > /// theDecimal - the decimal separator
                        >
                        > function isThousands(pos ition) {
                        > if (Math.floor(pos ition/3)*3==position) return true;
                        > return false;
                        > };
                        >
                        > function formatMoney (theNumber,theC urrency,theThou sands,theDecima l) {
                        > var theDecimalDigit s =
                        > Math.round((the Number*100)-(Math.floor(the Number)*100));
                        > if (theDecimalDigi ts < 10) {theDecimalDigi ts = "0"+theDecimalD igits};
                        > theDecimalDigit s= ""+ (theDecimalDigi ts + "0").substring( 0,2);
                        > theNumber = ""+Math.floor(t heNumber);
                        > var theOutput = theCurrency;
                        > for (x=0; x<theNumber.len gth; x++) {
                        > theOutput += theNumber.subst ring(x,x+1);
                        > if (isThousands(th eNumber.length-x-1) && (theNumber.leng th-x-1 !=
                        > 0)) {
                        > theOutput += theThousands;
                        > };
                        > };
                        > theOutput += theDecimal + theDecimalDigit s;
                        > return theOutput;
                        > };
                        >
                        > </script>
                        >
                        > --
                        > Dennis M. Marks
                        > http://www.dcs-chico.com/~denmarks/
                        > Replace domain.invalid with dcsi.net
                        >
                        >
                        > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                        > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                        > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


                        Comment

                        • Dennis M. Marks

                          #13
                          Re: Formatting Number With Thousands Separator - corrected

                          In article <40763394$0$276 42$61ce578d@new s.syd.swiftdsl. com.au>,
                          Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:

                          <snip>[color=blue]
                          > on a second issue, consider the following:
                          >
                          > --------------------------------
                          > area cost/m² total
                          > --------------------------------
                          > area1 100 $13.65 $1,365
                          > area2 1000 $14.52 $14,520
                          > area3 10000 $17.49 $174,900
                          > --------------------------------
                          > grand total: $190,785
                          > --------------------------------
                          >
                          > the grand total now comes up with NaN (not a number i assume?)
                          >
                          > even though the formula reads:
                          >
                          > gtotal = parseFloat(area 1) + parseFloat(area 2) + parseFloat(area 3);
                          >
                          > not sure if i should be using parseFloat or ParseInt...
                          >
                          > as I mentioned i dont have a c/jscript background so some of this is a
                          > little unclear to me.
                          >
                          >
                          >
                          > thanks again for your help :)
                          > -Douglas
                          >
                          >[/color]
                          Parsing seems to stop at the first non numeric character which is the
                          dollar sign. You may need to display formatted numbers after
                          calculations. Maybe keep 2 sets, one formatted and one not. I hope
                          someone has a better answer. I have never used parseInt or parseFloat
                          until I just tested it now.

                          BTW: Post responses at the bottom rather than the top.

                          --
                          Dennis M. Marks

                          Replace domain.invalid with dcsi.net


                          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                          Comment

                          • Douglas

                            #14
                            Re: Formatting Number With Thousands Separator - corrected


                            "Dennis M. Marks" <denmarks@domai n.invalid> wrote in message
                            news:0904200407 23038633%denmar ks@domain.inval id...[color=blue]
                            > In article <40763394$0$276 42$61ce578d@new s.syd.swiftdsl. com.au>,
                            > Douglas <post.to.the.gr oup@so.everyone .can.learn.com. au> wrote:
                            >
                            > <snip>[color=green]
                            > > on a second issue, consider the following:
                            > >
                            > > --------------------------------
                            > > area cost/m² total
                            > > --------------------------------
                            > > area1 100 $13.65 $1,365
                            > > area2 1000 $14.52 $14,520
                            > > area3 10000 $17.49 $174,900
                            > > --------------------------------
                            > > grand total: $190,785
                            > > --------------------------------
                            > >
                            > > the grand total now comes up with NaN (not a number i assume?)
                            > >
                            > > even though the formula reads:
                            > >
                            > > gtotal = parseFloat(area 1) + parseFloat(area 2) + parseFloat(area 3);
                            > >
                            > > not sure if i should be using parseFloat or ParseInt...
                            > >
                            > > as I mentioned i dont have a c/jscript background so some of this is a
                            > > little unclear to me.
                            > >
                            > >
                            > >
                            > > thanks again for your help :)
                            > > -Douglas
                            > >
                            > >[/color]
                            > Parsing seems to stop at the first non numeric character which is the
                            > dollar sign. You may need to display formatted numbers after
                            > calculations. Maybe keep 2 sets, one formatted and one not. I hope
                            > someone has a better answer. I have never used parseInt or parseFloat
                            > until I just tested it now.
                            >
                            > BTW: Post responses at the bottom rather than the top.
                            >
                            > --
                            > Dennis M. Marks
                            > http://www.dcs-chico.com/~denmarks/
                            > Replace domain.invalid with dcsi.net
                            >
                            >
                            > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                            > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                            > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]

                            No worries :)

                            upon further consideration, i realised i should probably make the grandtotal
                            formula like this:

                            gtotal = (area1 * cost1) + (area2 * cost2) + (area3 * cost3);

                            rather than summing the results of the calculated fields.

                            -Douglas






                            Comment

                            • Dr John Stockton

                              #15
                              Re: Formatting Number With Thousands Separator

                              JRS: In article <08042004145748 2581%denmarks@d omain.invalid>, seen in
                              news:comp.lang. javascript, Dennis M. Marks <denmarks@domai n.invalid>
                              posted at Thu, 8 Apr 2004 14:57:48 :
                              [color=blue]
                              >Lines: 183
                              > ... ... ...
                              >There is a bug that I am checking into. If the first digit after the
                              >decimal is zero it drops it.
                              > ...[/color]

                              You've been posting here long enough to have learnt how follow-up posts
                              should be formatted; it's explained in the FAQ.

                              Answer after what needs to be quoted; signatures almost never need be
                              reproduced.

                              See also via below.

                              --
                              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                              Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                              Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                              No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                              Comment

                              Working...