使用spark.formatters.NumberFormatter分隔符后不超过9位数?

时间:2011-07-13 09:41:47

标签: flex flex4

我必须在Flex-AdvancedDataGrid中显示非常小的值(电容) 我使用spark.formatters.NumberFormatter。 如果我使用3,6或9作为fractionalDigits,一切都很好。

但是如果我使用12,因为我需要十进制分隔符后面的12位数字,那么这个值会在9位数之后被删除!

有没有办法在分隔符后获得超过9位数。

或者有没有办法使用像“4.7 E-12”这样的格式(必须是E-9,E-12,E-15等)

2 个答案:

答案 0 :(得分:1)

toPrecision和toFixed最多可以工作20位数。多数民众赞成。 我将在此基础上编写一个函数来获得类似4.7 E-12的结果。

感谢您的帮助 扬

答案 1 :(得分:0)

我创建了a custom pattern formatter class,它允许您指定希望使用您想要的任何符号或结构格式化字符串/数字的方式。根据您的要求,这可能对您有所帮助。您可以根据需要随意修改此类。

<强> CustomPatternFormatter.as

package
{
    import mx.formatters.Formatter;

    public class CustomPatternFormatter extends Formatter
    {
        private static const VALID_PATTERN_CHARACTERS:String = "#,.-";

        // Use # as a placeholder for a regular
        // character in the input string.
        // Then add other characters between the
        // # symbol for the desired output format.
        // ex. The pattern ##,##,##.## with input 12345678
        // will output 12,34,56.78
        public var formatPattern:String;

        // If True, the input string must match the number
        // of # characters in the formatPattern.
        public var inputMustMatchPatternLength:Boolean;

        //Constructor
        public function CustomPatternFormatter()
        {
            super();
            formatPattern = "";
            inputMustMatchPatternLength = false;
        }

        // Override format().
        override public function format(value:Object):String
        {
            // Reset error if it exists.
            if (error)
                error = null;

            // If value is null, or empty String just return ""
            // but treat it as an error for consistency.
            // Users will ignore it anyway.
            if (!value || (value is String && value == ""))
            {
                error = "Cannot convert an empty value";
                return "";
            }

            // Check to see if the input value must match the format pattern
            if (inputMustMatchPatternLength && String(value).length != countOccurrences(formatPattern, "#"))
            {
                error = "The input value length does not match the format pattern length.";
                return "";
            }

            // If the value is valid, format the string.
            var fStrLen:int = 0;
            var letter:String;
            var n:int;
            var i:int;
            var v:int;

            // Make sure the formatString is valid.
            n = formatPattern.length;
            for (i = 0; i < n; i++)
            {
                letter = formatPattern.charAt(i);
                if (letter == "#")
                {
                    fStrLen++;
                }
                else if (VALID_PATTERN_CHARACTERS.indexOf(letter) == -1)
                {
                    error = "You can only use the following symbols in the formatPattern: " + VALID_PATTERN_CHARACTERS;
                    return "";
                }
            }

            var returnString:String = "";
            var vStr:String = String(value).replace(".", "").split("").reverse().join("");
            var fArr:Array = formatPattern.split("").reverse();
            var fChar:String;

            // Format the string
            for (v = 0; v < vStr.length; v++)            {                   if (fArr.length > 0)
                {
                    do
                    {
                        fChar = fArr.shift();

                        if (fChar != "#")
                            returnString += fChar;

                    } while (fChar != "#" && fArr.length > 0);
                }

                returnString += vStr.charAt(v);
            }

            // Return the formatted string
            return returnString.split("").reverse().join("");
        }

        protected function countOccurrences(str:String, char:String):int
        {
            var count:int = 0;
            for (var i:int=0; i < str.length; i++)
            {
                if (str.charAt(i) == char)
                {
                    count++;
                }
            }
            return count;
        }
    }
}