⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numberformat.as

📁 actionscript3 cookbook 源代码S3CBLibrary
💻 AS
📖 第 1 页 / 共 2 页
字号:
              sNumber += aPart1[i];
            }
          }
        }
      }

      // Return the number string.
      return sNumber;
    }

    /**
     *  Format a number as currency. This method works very similarly to the 
     *  standard format( ) method.
     *  <p>
     *  Example usage: <br />
     *  trace(nfFormatter.currencyFormat(1000)); // Displays $1,000.00 <br />
     *  trace(nfFormatter.currencyFormat(1000, new Locale("fr")));  // Displays: 1.000,00€ <br />
     *  trace(nfFormatter.currencyFormat(1000, {group: "|", decimal: "%", before: true, currency: "^"}));  // Displays: ^1|000%00 <br />
     *  </p>
     *  to the nearest of a specified interval.
     *  @param  number          The number you want to format.
     *  @param  locale          (optional) A Locale object.
     *  @param  symbols object  (optional) An object specifying the group, decimal, and currency symbols.
     *                          Can also include a before (Boolean) property specifying whether or not
     *                          ths currency symbol should be placed at the beginning or end.
     *  @return                 The formatted number as a string.
     */
    public function currencyFormat(nAmount:Number, oParameter1:Object = null):String {

      // If the locale is passed to the method, use that. Otherwise, create a new,
      // default Locale object.
      var lStyle:Locale = (arguments[1] is Locale) ? arguments[1] : new Locale();

      // If the symbols object is passed to the method, use that. Otherwise, retrieve
      // the symbols object based on the locale.
      if(oParameter1 != null && oParameter1.hasOwnProperty("group")) {
        var oSymbols:Object = oParameter1;
      }
      else {
        var oSymbols:Object = getSymbols(true, lStyle);
      }

      var sCurrencySymbol:String = oSymbols.currency;
      var sGroup:String = oSymbols.group;
      var sDecimal:String = oSymbols.decimal;
      var sTempMask:String = _sMask;
      _sMask = null;

      // Create a Locale object that uses US formatting, then format the
      // amount using that locale.
      var lLocale:Locale = new Locale();
      lLocale.language = "en";
      lLocale.variant = "US";
      var sAmount:String = format(nAmount, lLocale);  
      _sMask = sTempMask;

      // Split the formatter string into parts using the dot as the
      // delimiter.
      var aParts:Array = sAmount.split(".");

      // If there were no decimal places, use a default of 00. Otherwise,
      // Round the decimal places to two.
      if(aParts[1] == undefined) {
        aParts[1] = "00";
      }
      else {
        aParts[1] = Number(aParts[1]);
        var nPart1Length:Number = String(aParts[1]).length;
        if(nPart1Length > 2) {
          aParts[1] /= Math.pow(10, (nPart1Length - 2));
          aParts[1] = Math.round(aParts[1]);
        }
      }

      // Join the parts pack to a new string. Then split that into an array
      // of characters.
      sAmount = aParts.join(".");
      var aAmount:Array = sAmount.split("");

      // Loop through each of the elements of the array, and replace commas
      // with the appropriate grouping symbol and dots with the appropriate
      // decimal marker.
      for(var i:Number = 0; i < aAmount.length; i++) {
        if(aAmount[i] == ",") {
          aAmount[i] = sGroup;
        }
        else if(aAmount[i] == ".") {
          aAmount[i] = sDecimal;
        }
      }

      // Add the current symbol.
      var sReturnString:String = ((oSymbols.before) ? sCurrencySymbol : "") + aAmount.join("") + ((!oSymbols.before) ? sCurrencySymbol : "");

      // Return the string.
      return sReturnString;
    }

    /**
     *  Parse a string to a number. Numbers can be parsed using localized
     *  settings.
     *  <p>
     *  Example usage: <br />
     *  trace(nfFormatter.currencyFormat(1000)); // Displays $1,000.00 <br />
     *  trace(nfFormatter.currencyFormat(1000, new Locale("fr")));  // Displays: 1.000,00€ <br />
     *  trace(nfFormatter.currencyFormat(1000, {group: "|", decimal: "%", before: true, currency: "^"}));  // Displays: ^1|000%00 <br />
     *  </p>
     *  to the nearest of a specified interval.
     *  @param  number          The number string you want to parse.
     *  @param  radix           (optional) The radix to use when parsing the string. 10 is the default.
     *  @param  currency        (optional) A Boolean indicating whether or not the number string is formatted
     *                          as currency. The default is false.
     *  @param  locale          (optional) A Locale object.
     *  @return                 The number.
     */
    public function parse(sNumber:String, nRadix:Number, bCurrency:Boolean, lStyle:Locale):Number {

      // If the locale parameter is unspecified, use a default Locale object.
      if(lStyle == null) {
        lStyle = new Locale();
      }

      // Get the symbols.
      var oSymbols:Object = getSymbols(bCurrency, lStyle);

      // Split the string into an array of characters.
      var aCharacters:Array = sNumber.split("");

      // If the radix is undefined then use default radix interpretation.
      if(isNaN(nRadix)) {

        // If the first two characters are 0x, use a radix of 16. If the
        // first character is 0, use a radix of 8. If the first character
        // is a # then use a radix of 16. Otherwise use the default radix of
        // 10.
        if(aCharacters[0] == "O") {
          if(aCharacters[1] == "x") {
            nRadix = 16;
          }
          else {
            nRadix = 8;
          }
        }
        else if(aCharacters[0] == "#") {
          nRadix = 16;
        }
        else {
          nRadix = (isNaN(nRadix)) ? 10 : nRadix;
        }
      }

      // Loop through each character. If the character is a digit, don't do anything.
      // If the character is a decimal point, replace it with a dot. If the radix
      // Is greater than 10, allow alphabetic characters to remain. Otherwise, remove
      // the character from the array.
      for(var i:Number = 0; i < aCharacters.length; i++) {
        switch(aCharacters[i]) {
          case "0":
          case "1":
          case "2":
          case "3":
          case "4":
          case "5":
          case "6":
          case "7":
          case "8":
          case "9":
            break;
          case oSymbols.decimal:
            aCharacters[i] = ".";
            break;
          default:
            if(nRadix > 10) {
              if((aCharacters[i].charCodeAt(0) > 64 && aCharacters[i].charCodeAt(0) < 91) || (aCharacters[i].charCodeAt(0) > 96 && aCharacters[i].charCodeAt(0) < 123)) {
                break;
              }
            }
            aCharacters.splice(i, 1);
            i--;
        }
      }

      // If the radix is 10, simply return the array of characters joined and
      // cast as a number. Otherwise, use parseInt( ).
      if(nRadix == 10) {
        return Number(aCharacters.join(""));
      }
      else {
        return parseInt(aCharacters.join(""), nRadix);
      }
    }

    // This method is used to retreive grouping, decimal, and currency symbols
    // based on the locale.
    private function getSymbols(bCurrency:Boolean, lStyle:Locale):Object {
      var oSymbols:Object = new Object();
      switch(lStyle.languageVariant) {
        case "en-US":
          oSymbols.currency = "$";
          oSymbols.group = ",";
          oSymbols.decimal = ".";
          oSymbols.before = true;
          break;
        case "en-UK":
          oSymbols.currency = "\u00A3";
          oSymbols.group = ",";
          oSymbols.decimal = ".";
          oSymbols.before = true;
          break;
        case "es-MX":
          oSymbols.currency = "$";
          oSymbols.group = ",";
          oSymbols.decimal = ".";
          oSymbols.before = true;
          break;
        case "es-ES":
          oSymbols.currency = "\u20AC";
          oSymbols.group = ".";
          oSymbols.decimal = ",";
          oSymbols.before = false;
          break;
        case "fr":
          oSymbols.currency = "\u20AC";
          oSymbols.group = ".";
          oSymbols.decimal = ",";
          oSymbols.before = false;
          break;
        case "sv":
          oSymbols.currency = "kr";
          oSymbols.group = bCurrency ? "," : " ";
          oSymbols.decimal = ".";
          oSymbols.before = false;
          break;
        case "jp":
          oSymbols.currency = "\u200A5";
          oSymbols.group = ",";
          oSymbols.decimal = ".";
          oSymbols.before = true;
          break;
        case "nl":
          oSymbols.currency = "€";
          oSymbols.group = ".";
          oSymbols.decimal = ",";
          oSymbols.before = true;
          break; 
        default:
          oSymbols.currency = "\u20AC";
          oSymbols.group = ".";
          oSymbols.decimal = ",";
          oSymbols.before = true;
          break;
      }
      return oSymbols;
    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -