tag:blogger.com,1999:blog-1955739017412408985.post2785202008119402435..comments2023-06-21T08:29:32.288-07:00Comments on The Hard Way: Flash / ActionScript: String padding (leading zero)metric152http://www.blogger.com/profile/11587192756320730861noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-1955739017412408985.post-56557354779943339132011-07-24T07:16:09.262-07:002011-07-24T07:16:09.262-07:00Here's the version with types included:
/**
...Here's the version with types included:<br /><br />/**<br /> * This function will pad the left or right side of any variable passed in<br /> * elem [AS object]<br /> * padChar: String<br /> * finalLength: Number<br /> * dir: String<br /> *<br /> * return String<br /> */<br /> public function padValue(elem : Object, padChar :String , finalLength : Number, dir: String) : String<br /> {<br /> //make sure the direction is in lowercase<br /> dir = dir.toLowerCase();<br /><br /> //store the elem length<br /> var elemLen : uint = elem.toString().length;<br /><br /> //check the length for escape clause<br /> if(elemLen >= finalLength)<br /> {<br /> return elem.toString();<br /> }<br /><br /> //pad the value<br /> switch(dir)<br /> {<br /> default:<br /> case 'l':<br /> return padValue(padChar + elem, padChar, finalLength, dir);<br /> break;<br /> case 'r':<br /> return padValue(elem + padChar, padChar, finalLength, dir);<br /> break;<br /> }<br /> }<br /><br />Thanks for the snippet!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-40435598922254504412011-06-28T22:09:59.875-07:002011-06-28T22:09:59.875-07:00@TharosTheDragon That works also, but it was partl...@TharosTheDragon That works also, but it was partly an exercise in recursion.metric152https://www.blogger.com/profile/11587192756320730861noreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-66315366960133982262011-06-28T20:06:47.647-07:002011-06-28T20:06:47.647-07:00How's this?
function padString(string:String,...How's this?<br /><br />function padString(string:String, padChar:String, finalLength:int, padLeft:Boolean = true):String {<br /><br /> while (string.length < finalLength) {<br /><br /> string = padLeft ? padChar + string : string + padChar;<br /><br /> }<br /><br /> return string;<br /><br />}Rickeyhttps://www.blogger.com/profile/07614431252306863063noreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-57319428547040724672011-01-03T05:07:28.297-08:002011-01-03T05:07:28.297-08:00Thank you for the code.Thank you for the code.torrent downloadhttp://www.areze.comnoreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-4515742580146791042010-09-23T04:58:27.938-07:002010-09-23T04:58:27.938-07:00thanks for the code! :)thanks for the code! :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-39830352600800548752010-05-30T11:38:02.537-07:002010-05-30T11:38:02.537-07:00Maybe, but it's good to flex your recursion mu...Maybe, but it's good to flex your recursion muscles every once in awhile.metric152https://www.blogger.com/profile/11587192756320730861noreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-11708598172210290022010-05-30T11:31:34.788-07:002010-05-30T11:31:34.788-07:00Using recursion for a simple problem like adding z...Using recursion for a simple problem like adding zeroes is a bit over the top.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-90387963801191017352010-05-03T09:22:57.045-07:002010-05-03T09:22:57.045-07:00Thanks for the idea. I've updated the function...Thanks for the idea. I've updated the function to reflect the suggestion.metric152https://www.blogger.com/profile/11587192756320730861noreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-91697362028136707352010-05-03T01:56:03.713-07:002010-05-03T01:56:03.713-07:00Maybe you should write
(elemLen >= finalLength)...Maybe you should write<br />(elemLen >= finalLength)<br />to prevent a no return of recursion if elemLen > finalLength due a missuse or error.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-74072524482319535562010-01-25T09:22:26.500-08:002010-01-25T09:22:26.500-08:00That's true. You can find it here:
http://live...That's true. You can find it here:<br />http://livedocs.adobe.com/flex/3/langref/mx/formatters/NumberFormatter.html<br /><br />It doesn't allow you to pad the number with zeros though.metric152https://www.blogger.com/profile/11587192756320730861noreply@blogger.comtag:blogger.com,1999:blog-1955739017412408985.post-78158409099435991362010-01-25T04:31:37.614-08:002010-01-25T04:31:37.614-08:00Flex has the NumberFormatter class...Flex has the NumberFormatter class...Anonymousnoreply@blogger.com