0

我编写了一个将数字拆分为逗号的小函数,但我必须多次运行该数字。你能给我一个更好的方法吗?

public static function getCommaString(value:Number):String {
  var stringValue:String = value.toString();
  if (stringValue.length <= 3) {
    return stringValue;
  }
  var i:int = stringValue.length % 3;
  if (i == 0) {
    i = 3;
  }
  for (; i < stringValue.length; i += 4 ) {
    var part1:String = stringValue.substr(0, i);
    var part2:String = stringValue.substr(i, stringValue.length);
    stringValue = part1.concat(",", part2);
  }
  return stringValue;
}
4

1 回答 1

0

我整理了一个测试类,用于比较您的解决方案和其他解决方案,包括来自此博客文章的解决方案和来自此相关StackExchange 问题的解决方案,并以一致的方式对它们进行格式化以便于比较:

package
{
    public class CommaNumberSolutions
    {   
        public static function commaify( input:Number ):String
        {
            var split:Array = input.toString().split( '.' ),
                front:String = split[0],
                back:String = ( split.length > 1 ) ? "." + split[1] : null,
                n:int = input < 0 ? 2 : 1,
                commas:int = Math.floor( (front.length - n) / 3 ),
                i:int = 1;

            for ( ; i <= commas; i++ )
            {
                n = front.length - (3 * i + i - 1);
                front = front.slice( 0, n ) + "," + front.slice( n );
            }

            if ( back )
                return front + back;
            else
                return front;
        }

        public static function getCommaString( input:Number ):String
        {
            var s:String = input.toString();

            if ( s.length <= 3 )
                return s;

            var i:int = s.length % 3;

            if ( i == 0 )
                i = 3;

            for ( ; i < s.length; i += 4 )
            {
                var part1:String = s.substr(0, i);
                var part2:String = s.substr(i, s.length);
                s = part1.concat(",", part2);
            }

            return s;
        }

        public static function formatNumber( input:Number ):String
        {
            var s:String = input.toString()
            var result:String = ''

            while ( s.length > 3 )
            {
                var chunk:String = s.substr(-3)
                s = s.substr(0, s.length - 3)
                result = ',' + chunk + result
            }

            if ( s.length > 0 )
                result = s + result

            return result
        }

        public static function commaCoder( input:Number ):String
        {
            var s:String = "";
            var len:Number = input.toString().length;

            for ( var i:int = 0; i < len; i++ )
            { 
                if ( (len-i) % 3 == 0 && i != 0)
                    s += ",";

                s += input.toString().charAt(i);
            }
            return s;
        }

        public static function regex1( input:Number ):String
        {
            return input.toString().replace( /-{0,1}(\d)(?=(\d\d\d)+$)/g, "$1," );
        }

        public static function regex2( input:Number ):String
        {
            return input.toString().replace( /-{0,1}\d{1,3}(?=(\d{3})+(?!\d))/g , "$&,")
        }

        public static function addCommas( input:Number ):String
        {
            var negative:String = "";
            if ( input < 0 )
            {
                negative = "-";
                input = Math.abs(input);
            }

            var s:String = input.toString();
            var results:Array = s.split(/\./);
            s = results[0];

            if ( s.length > 3 )
            {
                var mod:Number = s.length % 3;
                var output:String = s.substr(0, mod);
                for ( var i:Number = mod; i < s.length; i += 3 )
                {
                    output += ((mod == 0 && i == 0) ? "" : ",") + s.substr(i, 3);
                }

                if ( results.length > 1 )
                {
                    if ( results[1].length == 1 )
                        return negative + output + "." + results[1] + "0";
                    else
                        return negative + output + "." + results[1];
                }
                else
                    return negative + output;
            }
            if ( results.length > 1 )
            {
                if ( results[1].length == 1 )
                    return negative + s + "." + results[1] + "0";
                else
                    return negative + s + "." + results[1];
            }
            else
                return negative + s;
        }
    }
}

您可以在我的另一个答案中看到更详细的分析,但您可以比较每个解决方案的紧凑性并将其与它们的准确性和性能进行权衡,但是为了简洁起见,这个解决方案是最准确和性能最好的(尽管不是最紧凑的) :

public function commaify( input:Number ):String
{
    var split:Array = input.toString().split( '.' ),
        front:String = split[0],
        back:String = ( split.length > 1 ) ? "." + split[1] : null,
        n:int = input < 0 ? 2 : 1,
        commas:int = Math.floor( (front.length - n) / 3 ),
        i:int = 1;

    for ( ; i <= commas; i++ )
    {
        n = front.length - (3 * i + i - 1);
        front = front.slice( 0, n ) + "," + front.slice( n );
    }

    if ( back )
        return front + back;
    else
        return front;
}
于 2012-11-16T04:54:25.603 回答