15 Replies Latest reply on Jun 1, 2006 11:46 AM by Newsgroup_User

    Commas in text fields...

    DIY_Lobotomy Level 1
      I have a couple fields where people enter some numbers and then calculations are done (behind the scenes), and a resulting number appears in a new field. I need to know how to get commas to appear in the appropriate places (for instance - if someone's result was 82746, I'd want it to appear as "82,746" as opposed to "82746")...

      Here is a link to download my .FLA that I am working on. The code in question is on Frame 5. Thanks!:

      http://al-i.anixter.com/TEMP/pie2.fla

      --Hoffman
        • 1. Re: Commas in text fields...
          2m Level 2
          Ooops,

          I opend the file, but I couldn't go on, as I was afraid of being laid off becaus I do unproductive labour ;-)

          Just kidding, I'll have look!
          • 2. Re: Commas in text fields...
            DIY_Lobotomy Level 1
            Haha - thanks! I'd really appreciate it!!!....
            • 3. Re: Commas in text fields...
              boyBacon
              try this function

              • 4. Re: Commas in text fields...
                DIY_Lobotomy Level 1
                Okay - we're getting MUCH closer. That code ALMOST works, but can you somehow ammed it to account for the fact that these are dollars (and not just numbers), so the comma'ing sequence needs to start AFTER the decimal (the "change")...

                ...like, right now that function would display 23876.45 as "23,876,.45" (with a comma before the decimal). Am I making sense? And is it easy to change the code to account for that? Thanks!!!
                • 5. Re: Commas in text fields...
                  2m Level 2
                  actually I had a similar function writen once, that took care of none natural number too, as I couldn't find it, iI was in the process of recreating it.

                  Seems someone was faster than me I'm really not productive enough ;-)
                  • 6. Re: Commas in text fields...
                    2m Level 2
                    Ok, i read your reply after my post, so I'll finish my version.
                    • 7. Re: Commas in text fields...
                      DIY_Lobotomy Level 1
                      Nevermind - I figured it out...

                      You just have to change the:

                      for (var i=num_str.length-3; i>0; i-=3) {

                      to:

                      for (var i=num_str.length-6; i>0; i-=3) {

                      Thanks for all the help, guys!!!...
                      • 8. Re: Commas in text fields...
                        2m Level 2
                        function addDiv(inputNumber, div) {
                        // convert to string
                        var strNum = inputNumber.toString();
                        // save value after . to store for appending again later if necessary
                        hasRest = false;
                        if (inputNumber != Math.floor(inputNumber)) {
                        var rest = strNum.split(".")[1];
                        hasRest = true;
                        }
                        var full = strNum.split(".")[0];
                        isNegative = false;
                        if (inputNumber<0) {
                        isNegative = true;
                        full = full.slice(1);
                        }
                        for (var i = full.length-3; i>0; i -= 3) {
                        var pre = full.substr(0, i);
                        var post = full.substr(i, full.length-i);
                        full = pre+div+post;
                        }
                        if (isNegative) {
                        full = "-"+full;
                        }
                        if (hasRest) {
                        full = full+"."+rest;
                        }
                        return full;
                        }
                        trace(addDiv(-34567466.45, ","));
                        • 9. Re: Commas in text fields...
                          Level 7
                          But what if you don't have a decimal? Or what if there's 3 decimal places?

                          function commatize(theString:String):String {
                          var numOut:String = "";
                          var l = theString.indexOf(".");
                          if (l != -1) {
                          numOut += theString.substr(l, theString.length - l);
                          } else {
                          l = theString.length;
                          }
                          while (l > 3) {
                          numOut = "," + theString.substr(l - 3, 3) + numOut;
                          l -= 3;
                          }
                          return theString.substr(0, l) + numOut;
                          }




                          --
                          Dave -
                          Adobe Community Expert
                          www.blurredistinction.com
                          www.macromedia.com/support/forums/team_macromedia/


                          • 10. Re: Commas in text fields...
                            2m Level 2
                            Hi Dave,

                            I dont know of your question relates to my suggestion or not, but I'm pretty sure, that my script works in both cases. Your's is defnatly more elegant as it is shorter, but it lacks support for negative numbers, and I tried to focus on understability rather than on shortness ;-)
                            • 11. Re: Commas in text fields...
                              Level 7
                              I didn't even think of negative numbers... And I wasn't necessarily going
                              for shortness either. But, since you mentioned it. This is both shorter, and
                              handles negative numbers:


                              function commatize(theString:String):String {
                              var numOut:String = "";
                              var l = (theString.indexOf(".") != -1) ? theString.indexOf(".") :
                              theString.length;
                              numOut += theString.substr(l, theString.length - l);
                              while (l > 3) {
                              numOut = (theString.substr(l - 4, 1) != "-") ? "," + theString.substr(l -
                              3, 3) + numOut : theString.substr(l - 3, 3) + numOut;
                              l -= 3;
                              }
                              return theString.substr(0, l) + numOut;
                              }



                              --
                              Dave -
                              Adobe Community Expert
                              www.blurredistinction.com
                              www.macromedia.com/support/forums/team_macromedia/


                              • 12. Re: Commas in text fields...
                                2m Level 2
                                You're the man!

                                I surrender ;-)

                                btw how didi you come by your title of "Adobe Community Expert" did they (the mystirious ones) contact you on night in a dak ally or did you have to apply?

                                (Not that I feel a calling to invade the ranks, but I followed on the "www.macromedia.com/support/forums/team_macromedia/" manly in the hope of seeing faces, but found only vague statements...)
                                • 13. Re: Commas in text fields...
                                  Level 7
                                  >>manly in the hope of seeing faces, but found only vague statements...)

                                  There are faces there... From the link, click the "Find an Adobe Community
                                  Expert" at the bottom... click peoples names from there to see their bios.

                                  It's a program you can ask to be in I believe, but I think someone needs to
                                  leave for someone else to join. There should be more info at that link.


                                  --
                                  Dave -
                                  Adobe Community Expert
                                  www.blurredistinction.com
                                  www.macromedia.com/support/forums/team_macromedia/


                                  • 14. Re: Commas in text fields...
                                    2m Level 2
                                    Thanks for the info, and sorry that I didn't look well enough by myself.

                                    btw. you are a lucky one, if negative numbers don't come to your mind when calculating with dollars. But maybe no wonder with Einstein and Newton around ;-)
                                    • 15. Re: Commas in text fields...
                                      Level 7
                                      >>btw. you are a lucky one, if negative numbers don't come to your mind when
                                      calculating with dollars. But maybe no wonder with Einstein and Newton
                                      around
                                      ;-)


                                      HEHE.. Naw, I simply overlooked it. I wish I were so lucky. All I need do
                                      these days is look in my bank account to see negative numbers...

                                      --
                                      Dave -
                                      Adobe Community Expert
                                      www.blurredistinction.com
                                      http://www.adobe.com/communities/experts/