Flash create comma from number performance -


I wrote a small function that divides a number into commas, but I sometimes have to run on numbers. Can you choose a better way of doing this?

  Public stable work getCommaString (value: number): string {var stringValue: String = value.toString (); If (stringValue.length & lt; = 3) {return stringValue; } Var i: int = stringValue.length% 3; If (i == 0) {i = 3; }; (; I    

I have a test class that compares your solution and other solutions Including this and its related solutions and have been formulated in a consistent manner for easy comparison:

  package {public class CommaNumberSolutions {public static function comma (Input: number): string {Var split: Array = Input.toString (). Split ('.'), Front: string = partition [0], back: string = (split.length> 1)? "." + Partition [1]: zero, n: int = input & lt; 0? 2: 1, comma: int = Math.floor ((front.length - n) / 3), i: int = 1; For (; i & lt; = commas; i ++) {n = front. Lamba - (3 * i + i - 1); Front = front Salis (0, n) + "," + front. Slice (n); } If (back) returns return + back; And return back; } Public stable work getCommaString (Input: number): string {var s: string = input.toString (); If (s.length & lt; = 3) returns; Var i: int = s.length% 3; If (I == 0) I = 3; For (; i & lt; s.length; i + = 4) {var part 1: string = s.substr (0, i); Var part2: string = s.substr (i, s.length); S = part1.concat (",", part2); } Return S; } Public Static Function Format Name (Input: Number): String {var s: string = input.toString () var results: string = '' while (s.length & gt; 3) {var chunk: string = s.substr ( - 3) s = s.substr (0, s.length - 3) result = ',' + chuck + result} if (s.length & gt; result) result = s + result returns result} public static work comma ( Input: number): string {var s: string = ""; Var len: number = input.toString (). Length; For (var i: int = 0; i  1) {if ( Results [1] .length == 1) Negative + s + "." + Results [1] + "0"; And negative + s + "." + Results [1];} Again negative + S;}} }   

You can see more detailed analytics, but you can compare the compaction of each solution and lose weight against its accuracy and performance, although no Shiptta this solution is the most accurate and executable (though not the most compact):

  public function Comaiaf (input number): String {var split: Array = input.toString (). Split ('.'), Front: string = partition [0], back: string = (partition.label> gt; 1)? "." + Partition [1]: zero, n: int = input & lt; 0? 2: 1, comma: int = Math.floor ((front.length - n) / 3), i: int = 1; For (; i & lt; = commas; i ++) {n = front. Lamba - (3 * i + i - 1); Front = front Salis (0, n) + "," + front. Slice (n); } If (back) returns return + back; And return back; }    

Comments