9 Replies Latest reply on Oct 27, 2006 1:53 AM by FasterPastor

    Display a number with commas

    FasterPastor Level 1
      Here's a way to display a number with commas. See attached code.

      I had searched here for help on how to do it, and couldn't find anything. So, then I wrote this. I'm a hack, self-taught coder, so there may be a more eloquent way, but this does work.
        • 1. Re: Display a number with commas
          Level 7
          Cool.

          Just in case you're interested in that more elegant way...or at least
          shorter...

          on DisplayNumberWithCommas str
          numberAsString = (str.integer).string
          newNumberAsString = ""
          cnt = numberAsString.length
          loopCnt = 1
          repeat with x = cnt down to 1
          put numberAsString.char [x] before newNumberAsString
          if (loopCnt mod 3 = 0) and (x <> 1) then
          put "," before newNumberAsString
          end if
          loopCnt = loopCnt + 1
          end repeat

          return newNumberAsString

          end


          --
          Craig Wollman
          Word of Mouth Productions

          phone 212 928 9581
          fax 212 928 9582
          159-00 Riverside Drive West #5H-70
          NY, NY 10032
          www.wordofmouthpros.com


          "FasterPastor" <webforumsuser@macromedia.com> wrote in message
          news:ehpee1$fl0$1@forums.macromedia.com...
          > Here's a way to display a number with commas. See attached code.
          >
          > I had searched here for help on how to do it, and couldn't find anything.
          > So,
          > then I wrote this. I'm a hack, self-taught coder, so there may be a more
          > eloquent way, but this does work.
          >
          > -- Here's how to call the handler and store its resulting "commas
          > inserted"
          > number-as-text string
          > -- in either a text or field member named "Score" ... assumed you have a
          > global variable named
          > -- "gScore" that has a number in it that you want displayed with commas.
          > -- You could put this line into an "on exitframe" handler in either a
          > movie
          > script or a sprite's behavior script:
          >
          > member("Score").text = "Score: " & displayNumberWithCommas(gScore)
          >
          > -- here's the handler itself, that got called by the above
          > on displayNumberWithCommas(tNumber)
          > -- applies "commas" to the number for proper display
          > -- returns a string
          > NumberAsString = string(integer(tNumber))
          > StringLength = NumberAsString.length
          > if StringLength > 3 then
          > if StringLength/3.0 > integer(StringLength/3) then
          > NumberOfCommas = integer(StringLength/3)
          > else
          > NumberOfCommas = integer(StringLength/3)-1
          > end if
          > else
          > NumberOfCommas = 0
          > end if
          > NewNumberAsString = ""
          > digit = 1
          > repeat while StringLength
          > NewNumberAsString = NumberAsString.char[StringLength] &
          > NewNumberAsString
          > if digit = 3 and NumberOfCommas > 0 then
          > NewNumberAsString = "," & NewNumberAsString
          > NumberOfCommas = NumberOfCommas - 1
          > digit = 1
          > else
          > digit = digit + 1
          > end if
          > StringLength = StringLength -1
          > end repeat
          > return NewNumberAsString
          > end
          >


          • 2. Re: Display a number with commas
            Level 7
            And just in case you wanted an even shorter version:

            on addCommas vNumberAsString
            l=vNumberAsString.length
            if l<4 then return vNumberAsString
            return
            addCommas(vNumberAsString.char[1..l-3])&","&vNumberAsStringr.char[l-2..l]
            end
            • 3. Re: Display a number with commas
              FasterPastor Level 1
              Craig and Mike: Thanks

              Mike: Is "return" supposed to be on a line by itself?

              Also, can you explain to me a little of how that works? What is the the ".." syntax?

              It may be just me, the "I" or "I" (I cannot tell if that is a lower case L or an upper case i) seems to blend in with the 1 and it gets a little hard to follow, especially when I don't know what the elipsis-looking ".." does. Sorry to be daft. Can you give me a clue how that works?

              • 4. Re: Display a number with commas
                duckets Level 1
                The .. indicates a range of characters.

                myString.char[1..3] gives you characters 1 to 3 of 'myString'

                Mike's code is a recursive function, which means it calls itself repeatedly with the 'remainder' of the string to break into 3's, until the remainder of the string is less than 4 characters in length. Quite nifty!

                However all of these have flaws :-)
                (see next post)

                - Ben

                • 5. Re: Display a number with commas
                  Level 7
                  No, the return and the following block of text should all be one a
                  single line. The forum software put it onto another line.

                  Those are lower case "L" characters (stands for Length). And yeah, they
                  look like 1s. Feel free to change the l to anything you wish.

                  the .. means "until". something.char[1..3] means get the 1st 3
                  characters of something. For instance:

                  put "mike"
                  -- "mike"

                  put "mike".char[1]
                  -- "m"

                  put "mike".char[1..3]
                  -- "mik"
                  • 6. Re: Display a number with commas
                    duckets Level 1
                    quote:

                    Originally posted by: FasterPastor
                    I had searched here for help on how to do it, and couldn't find anything.


                    You should have asked! here's one I have had sitting around for a while. (code attached below). It handles lots of cases which the other handlers posted so far do not. Specifically:

                    Negative numbers - input: -123456
                    - craig's: -,123,456
                    - dougs's: -,123,456
                    - mikes's: -,123,456
                    - duck's: -123,456

                    Floating point numbers - input: pi
                    - craig's: 3
                    - dougs's: 3
                    - mikes's: 3.1,416
                    - duck's: 3.1416

                    Numbers larger than the maxinteger - input: 149668992000
                    - craig's: -,654,863,360
                    - dougs's: -,654,863,360
                    - mikes's: 14,966,899,200,0.0,000
                    - duck's: 149,668,992,000

                    Negative floats - input: -123456.7890
                    - craig's: -,123,457
                    - dougs's: -,123,457
                    - mikes's: -12,345,6.7,890
                    - duck's: -123,456.789

                    Floats larger than the maxinteger - input: 2233445566.7788
                    - craig's: -2,061,521,729
                    - dougs's: -2,061,521,729
                    - mikes's: 223,344,556,6.7,788
                    - duck's: 2,233,445,566.7788

                    Yes.. the code is longer, but if you want to print something like the distance in meters to the sun, you need to handle floats properly!

                    enjoy ;-)

                    - Ben

                    • 7. Re: Display a number with commas
                      Level 7
                      Nice, I'm always happy to be humbled.

                      One more point. If you're going ot use a return statement I like to keep it
                      at the very end of the function. So, in a conditional statement, setting a
                      local variable to the value to be returned allows you to return that local
                      variable and always keep the return statement as the last line. It just
                      saves a little searching around for returns in longer functions. Just a
                      style choice not necessarily better.

                      --
                      Craig Wollman
                      Word of Mouth Productions

                      phone 212 928 9581
                      fax 212 928 9582
                      159-00 Riverside Drive West #5H-70
                      NY, NY 10032
                      www.wordofmouthpros.com


                      "duckets" <webforumsuser@macromedia.com> wrote in message
                      news:ehqgpm$pat$1@forums.macromedia.com...
                      >
                      quote:

                      Originally posted by: FasterPastor
                      > I had searched here for help on how to do it, and couldn't find
                      > anything.

                      >
                      > You should have asked! here's one I have had sitting around for a while.
                      > (code
                      > attached below). It handles lots of cases which the other handlers posted
                      > so
                      > far do not. Specifically:
                      >
                      > Negative numbers - input: -123456
                      > - craig's: -,123,456
                      > - dougs's: -,123,456
                      > - mikes's: -,123,456
                      > - duck's: -123,456
                      >
                      > Floating point numbers - input: pi
                      > - craig's: 3
                      > - dougs's: 3
                      > - mikes's: 3.1,416
                      > - duck's: 3.1416
                      >
                      > Numbers larger than the maxinteger - input: 149668992000
                      > - craig's: -,654,863,360
                      > - dougs's: -,654,863,360
                      > - mikes's: 14,966,899,200,0.0,000
                      > - duck's: 149,668,992,000
                      >
                      > Negative floats - input: -123456.7890
                      > - craig's: -,123,457
                      > - dougs's: -,123,457
                      > - mikes's: -12,345,6.7,890
                      > - duck's: -123,456.789
                      >
                      > Floats larger than the maxinteger - input: 2233445566.7788
                      > - craig's: -2,061,521,729
                      > - dougs's: -2,061,521,729
                      > - mikes's: 223,344,556,6.7,788
                      > - duck's: 2,233,445,566.7788
                      >
                      > Yes.. the code is longer, but if you want to print something like the
                      > distance
                      > in meters to the sun, you need to handle floats properly!
                      >
                      > enjoy ;-)
                      >
                      > - Ben
                      >
                      >
                      >
                      > on stringNumber n
                      >
                      > outputString = ""
                      > inputString = string(n)
                      >
                      > if inputString.char[1] = "-" then
                      > negative = true
                      > delete inputString.char[1]
                      > else
                      > negative = false
                      > end if
                      >
                      > fraction = ""
                      >
                      > if inputString contains "e" then
                      >
                      > mantissa = inputString.char[1..(offset("e", inputString)-1)]
                      > exponent = value(inputString.char[(offset("e",
                      > inputString)+1)..inputString.length])
                      >
                      > decimalChar = mantissa.char[2]
                      > mantissa = mantissa.char[1] & mantissa.char[3..mantissa.length]
                      >
                      > if mantissa.length < exponent+1 then
                      > plainNumber = mantissa
                      > repeat while plainNumber.length < exponent+1
                      > put "0" after plainNumber
                      > end repeat
                      > else
                      > plainNumber = mantissa.char[1..(exponent+1)]
                      > fraction = mantissa.char[(exponent+2)..mantissa.length]
                      > end if
                      >
                      > else
                      >
                      > if offset(".", inputString)>0 then
                      > decimalChar = "."
                      > end if
                      > if offset(",", inputString)>0 then
                      > decimalChar = ","
                      > end if
                      >
                      > if offset(decimalChar, inputString)>0 then
                      > plainNumber = inputString.char[1..(offset(decimalChar,
                      > inputString)-1)]
                      > fraction = inputString.char[(offset(decimalChar,
                      > inputString)+1)..inputString.length]
                      > else
                      > plainNumber = inputString
                      > fraction = ""
                      > decimalChar = string(1.2).char[2]
                      > end if
                      >
                      > end if
                      >
                      > if decimalChar = "." then
                      > separatorChar = ","
                      > else
                      > separatorChar = "."
                      > end if
                      >
                      >
                      > repeat while plainNumber.char[1] = "0"
                      > delete plainNumber.char[1]
                      > end repeat
                      >
                      >
                      > repeat while plainNumber.length > 0
                      > if plainNumber.length > 3 then
                      > nextDigits =
                      > separatorChar&plainNumber.char[plainNumber.length-2..plainNumber.length]
                      > delete plainNumber.char[plainNumber.length-2..plainNumber.length]
                      > else
                      > nextDigits = plainNumber
                      > plainNumber = ""
                      > end if
                      > put nextDigits before outputString
                      > end repeat
                      >
                      > repeat while fraction.char[fraction.length] = "0"
                      > delete fraction.char[fraction.length]
                      > end repeat
                      >
                      > if fraction.length > 0 then
                      > put decimalChar&fraction after outputString
                      > end if
                      >
                      >
                      > if negative then
                      > put "-" before outputString
                      > end if
                      >
                      > return outputString
                      >
                      > end
                      >


                      • 8. Re: Display a number with commas
                        Lukewig Level 1
                        Yet another approach for you (shorter than the others). Create a new Javascript member (this requires Director Mx2004) and paste this into it (if you are not using Director MX2004 and its not for Shockwave, you could create a similar function using the free PregEx Xtra).

                        -- Luke
                        • 9. Display a number with commas
                          FasterPastor Level 1
                          Lukewig

                          Thanks. I don't have MX04 and probably won't use an Xtra just for this. But I am interested to learn of the options!

                          Ben

                          Thanks. However, when my score calculations happen to render something like 40,466.667, I want the score to read as 40,467. That alone (always scores, always positive, always integers) is all I needed mine to do, and thus I did not consider or code for all the other needs. However, having all those extra considerations in your handler makes it worth copying and using. But my certain critera mean yours comes up short. LOL. I am using yours now, but simply adding an extra argument, "AsInteger" to your handler, and adding the following at the start of it:

                          on stringNumber n, AsInteger

                          if AsInteger then n = n.integer

                          Thanks again to Craig, Mike, Ben, and Lukewig. I have learned more about coding in this simple thread than I have in weeks.