在 Flex/AS3 中,您会使用什么来对数字进行零填充?

发布于 2024-07-15 13:24:47 字数 432 浏览 10 评论 0原文

此内容的重复

在 Flex/AS3 中,您将使用什么在数字左侧填充零?

是否有相当于 printfNumberFormat 的功能?

我正在寻找这个或类似的最好的实现:

public function zeroPad(number:int, width:int):String {
    // number = 46, width = 4 would return "0046"
}

Duplicate of this one.

What would you use to pad zeroes to the left of a number in Flex/AS3?

Is there an equivalent to printf or NumberFormat that does this?

I'm looking for the nicest implementation of this or something similar:

public function zeroPad(number:int, width:int):String {
    // number = 46, width = 4 would return "0046"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

恋你朝朝暮暮 2024-07-22 13:24:48
public static function getFormatedValue(num:Number, roundDecimalPlace:Number=2, showLastZerosInDecimalPlaces:Boolean = false, decimalSeparator:String=".", thousandsSeparator:String=",", currency:String="$"):String
{
  //assigns true boolean value to neg in number less than 0
  var neg:Boolean = (num < 0);

  //make the number positive for easy conversion
  num = Math.abs(num)

  var roundedAmount:String = String(num.toFixed(roundDecimalPlace));

  //split string into array for dollars and cents
  var amountArray:Array = roundedAmount.split(".");
  var dollars:String = amountArray[0]
  var cents:String = amountArray[1]

  //create dollar amount
  var dollarFinal:String = ""
  var i:int = 0
  for (i; i < dollars.length; i++)
  {
    if (i > 0 && (i % 3 == 0 ))
    {
      dollarFinal = thousandsSeparator + dollarFinal;
    }

    dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
  }       

  //create Cents amount and zeros if necessary
  var centsFinal:String;

  if(showLastZerosInDecimalPlaces)
  {
    centsFinal = String(cents);

    var missingZeros:int = roundDecimalPlace - centsFinal.length;

    if (centsFinal.length < roundDecimalPlace)
    {
      for (var j:int = 0; j < missingZeros; j++) 
      {
        centsFinal += "0";
      }
    }
  }
  else
  {
    if(Number(cents) != 0)
    {
      centsFinal = String(String(Number("0."+cents)).split(".")[1]);
    }
    else
    {
      roundDecimalPlace = 0;
    }
  }

  var finalString:String = ""

  if (neg)
  {
    finalString = "-"+currency + dollarFinal
  } else
  {
    finalString = currency + dollarFinal
  }

  if(roundDecimalPlace > 0)
  {
    finalString += decimalSeparator + centsFinal;
  } 

  return finalString;
}
public static function getFormatedValue(num:Number, roundDecimalPlace:Number=2, showLastZerosInDecimalPlaces:Boolean = false, decimalSeparator:String=".", thousandsSeparator:String=",", currency:String="$"):String
{
  //assigns true boolean value to neg in number less than 0
  var neg:Boolean = (num < 0);

  //make the number positive for easy conversion
  num = Math.abs(num)

  var roundedAmount:String = String(num.toFixed(roundDecimalPlace));

  //split string into array for dollars and cents
  var amountArray:Array = roundedAmount.split(".");
  var dollars:String = amountArray[0]
  var cents:String = amountArray[1]

  //create dollar amount
  var dollarFinal:String = ""
  var i:int = 0
  for (i; i < dollars.length; i++)
  {
    if (i > 0 && (i % 3 == 0 ))
    {
      dollarFinal = thousandsSeparator + dollarFinal;
    }

    dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
  }       

  //create Cents amount and zeros if necessary
  var centsFinal:String;

  if(showLastZerosInDecimalPlaces)
  {
    centsFinal = String(cents);

    var missingZeros:int = roundDecimalPlace - centsFinal.length;

    if (centsFinal.length < roundDecimalPlace)
    {
      for (var j:int = 0; j < missingZeros; j++) 
      {
        centsFinal += "0";
      }
    }
  }
  else
  {
    if(Number(cents) != 0)
    {
      centsFinal = String(String(Number("0."+cents)).split(".")[1]);
    }
    else
    {
      roundDecimalPlace = 0;
    }
  }

  var finalString:String = ""

  if (neg)
  {
    finalString = "-"+currency + dollarFinal
  } else
  {
    finalString = currency + dollarFinal
  }

  if(roundDecimalPlace > 0)
  {
    finalString += decimalSeparator + centsFinal;
  } 

  return finalString;
}
此岸叶落 2024-07-22 13:24:48

零填充例程的非常简短的示例(AS2)...

    Convert = function(Minutes) {
       return ('00'+String(int(Minutes/60)%24)).substr(-2,2);
    }

Very short example of zero padding routine (AS2)...

    Convert = function(Minutes) {
       return ('00'+String(int(Minutes/60)%24)).substr(-2,2);
    }
计㈡愣 2024-07-22 13:24:48
/** 
 * originally by Chris Agiasotis @ http://agitatedobserver.com/as3-currency-formatter/
 * improved by Joseph Balderson @ http://www.joeflash.ca
 */
package
{
    public class CurrencyFormat
    {
        public function CurrencyFormat(){ }

        public function getCurrency(num:Number,
                    decimalSeparator:String=".",
                    decimalPlace:Number=2,
                    currency:String="$",
                    thousandsSeparator:String=","
                ):String
        {
            //assigns true boolean value to neg in number less than 0
            var neg:Boolean = (num < 0);

            //make the number positive for easy conversion
            num = Math.abs(num)

            var roundedAmount:String = String(num.toFixed(decimalPlace));

            //split string into array for dollars and cents
            var amountArray:Array = roundedAmount.split(".");
            var dollars:String = amountArray[0]
            var cents:String = amountArray[1]

            //create dollar amount
            var dollarFinal:String = ""
            var i:int = 0
            for (i; i < dollars.length; i++)
            {
                if (i > 0 && (i % 3 == 0 ))
                {
                    dollarFinal = thousandsSeparator + dollarFinal;
                }

                dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
            }   

            //create Cents amount and zeros if necessary
            var centsFinal:String = String(cents);

            var missingZeros:int = decimalPlace - centsFinal.length;

            if (centsFinal.length < decimalPlace)
            {
                for (var j:int = 0; j < missingZeros; j++) 
                {
                    centsFinal += "0";
                }
            }

            var finalString:String = ""

            if (neg)
            {
                finalString = "-"+currency + dollarFinal
            } else
            {
                finalString = currency + dollarFinal
            }

            if(decimalPlace > 0)
            {
                finalString += decimalSeparator + centsFinal;
            } 

            return finalString;
        }
    }
}
/** 
 * originally by Chris Agiasotis @ http://agitatedobserver.com/as3-currency-formatter/
 * improved by Joseph Balderson @ http://www.joeflash.ca
 */
package
{
    public class CurrencyFormat
    {
        public function CurrencyFormat(){ }

        public function getCurrency(num:Number,
                    decimalSeparator:String=".",
                    decimalPlace:Number=2,
                    currency:String="$",
                    thousandsSeparator:String=","
                ):String
        {
            //assigns true boolean value to neg in number less than 0
            var neg:Boolean = (num < 0);

            //make the number positive for easy conversion
            num = Math.abs(num)

            var roundedAmount:String = String(num.toFixed(decimalPlace));

            //split string into array for dollars and cents
            var amountArray:Array = roundedAmount.split(".");
            var dollars:String = amountArray[0]
            var cents:String = amountArray[1]

            //create dollar amount
            var dollarFinal:String = ""
            var i:int = 0
            for (i; i < dollars.length; i++)
            {
                if (i > 0 && (i % 3 == 0 ))
                {
                    dollarFinal = thousandsSeparator + dollarFinal;
                }

                dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
            }   

            //create Cents amount and zeros if necessary
            var centsFinal:String = String(cents);

            var missingZeros:int = decimalPlace - centsFinal.length;

            if (centsFinal.length < decimalPlace)
            {
                for (var j:int = 0; j < missingZeros; j++) 
                {
                    centsFinal += "0";
                }
            }

            var finalString:String = ""

            if (neg)
            {
                finalString = "-"+currency + dollarFinal
            } else
            {
                finalString = currency + dollarFinal
            }

            if(decimalPlace > 0)
            {
                finalString += decimalSeparator + centsFinal;
            } 

            return finalString;
        }
    }
}
素衣风尘叹 2024-07-22 13:24:48

我在 AS3 中维护了一个 printf:
不幸的是堆栈溢出不允许我发布链接,但如果谷歌代码的项目名称是 printf-as3

反馈总是受欢迎的。

--

http://code.google.com/p/printf-as3/

I do maintain a printf in AS3:
Unfortunately stack overflow won't let me post links, but if the google code's project name is printf-as3

Feedback is always welcome.

--

http://code.google.com/p/printf-as3/

千鲤 2024-07-22 13:24:48

一个非常紧凑的解决方案:

public function zeroPad(s:String,pad:int):String {
    for(;s.length<pad;s='0'+s);
    return s;
}

A very compact solution:

public function zeroPad(s:String,pad:int):String {
    for(;s.length<pad;s='0'+s);
    return s;
}
寄居人 2024-07-22 13:24:47
public function zeroPad(number:int, width:int):String {
   var ret:String = ""+number;
   while( ret.length < width )
       ret="0" + ret;
   return ret;
}
public function zeroPad(number:int, width:int):String {
   var ret:String = ""+number;
   while( ret.length < width )
       ret="0" + ret;
   return ret;
}
单身情人 2024-07-22 13:24:47

就性能而言,我更喜欢使用字符串常量和 substr,如下所示:

package {
    public class Helper {
        private static const _ZEROS:String = "0000000000000000000000000000000000000000"; // 40 zeros, shorten/expand as you wish

        /*
         * f: positive integer value
         * z: maximum number of leading zeros of the numeric part (sign takes one extra digit)
         */
        public static function uint_Zeropadded(f:uint, z:int = 0):String {
            var result:String = f.toString();
            while (result.length < z)
                result = _ZEROS.substr(0, z - result.length) + result;
            return result;
        }
    }
}

与多次一位一位数字扩展字符串的性能缺陷相比,字符串常量的足迹非常小。 在大多数情况下(本例中最多 40 个零),所有调用的指令数量都是相同的。

Performance-wise, I prefer using a String constant and substr, like this:

package {
    public class Helper {
        private static const _ZEROS:String = "0000000000000000000000000000000000000000"; // 40 zeros, shorten/expand as you wish

        /*
         * f: positive integer value
         * z: maximum number of leading zeros of the numeric part (sign takes one extra digit)
         */
        public static function uint_Zeropadded(f:uint, z:int = 0):String {
            var result:String = f.toString();
            while (result.length < z)
                result = _ZEROS.substr(0, z - result.length) + result;
            return result;
        }
    }
}

The String constant has very little footstamp compared to the performance draw-backs of extending the string one by one digit numerous times. In most cases (up to 40 zeros in this example) number of instructions is the same for all calls.

何其悲哀 2024-07-22 13:24:47

菲尔的 作为优雅的递归的变体:

public function zeroPad (number:String, width:int):String {
    if (number.length < width)
        return "0" + zeroPad(number, width-1);
    return number;
}

我不知道它的AS3 上的性能,但看起来确实更酷! :-)

Phil's variant as an elegant recursion:

public function zeroPad (number:String, width:int):String {
    if (number.length < width)
        return "0" + zeroPad(number, width-1);
    return number;
}

I don't know of its performance on AS3 but it sure looks cooler! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文