3

按照此处的说明更改我的操作系统的小数分隔符后:(http://blogmines.com/blog/2010/03/11/how-to-change-the-decimal-separator-in-excel-2010/),我想在 Flex 中显示一个使用逗号作为千位分隔符和小数分隔符的数字。听起来很简单,对吧?

我尝试使用 Flex 提供的三种不同的 NumberFormatter。一路走来,我了解到它们中的两个在同一个类中不能很好地与其他类一起使用,即使在声明变量时使用完全限定的类路径,所以我不得不将它们分成三个类,如下所示:

NF1 - spark.formatters.NumberFormatter

package dstrube
{
    import flash.globalization.NumberParseResult;
    import spark.formatters.NumberFormatter;
    public class NF1
    {
        public static function get(value:String):String{
            var nf1:NumberFormatter = new NumberFormatter();
            var result:NumberParseResult = nf1.parse(value);
            return nf1.format(result.value);
        }
    }
}

NF2 - flash.globalization.NumberFormatter

package dstrube
{
    import flash.globalization.NumberParseResult;
import flash.globalization.NumberFormatter;
    public class NF2
    {
        public static function get(value:String):String{
            var nf2:NumberFormatter = new NumberFormatter("");// LocaleID.DEFAULT = same outcome as without
            nf2.fractionalDigits = 2; //= same outcome as without
            nf2.trailingZeros = true;
            var result:NumberParseResult = nf2.parse(value);
            //nf2.parseNumber(value); = NaN
            return nf2.formatNumber(result.value)
        }
    }
}

NF3 - mx.formatters.NumberFormatter(已弃用)

package dstrube
{
    //import mx.formatters.NumberBaseRoundType;
    import mx.formatters.NumberFormatter;
    public class NF3
    {
    public static function get(value:String):String{
        var nf3:NumberFormatter = new NumberFormatter();
        //nf3.rounding = NumberBaseRoundType.NEAREST; //no effect in this case
        return nf3.format(value);
    }
}

}

最后,主要的

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="init()"
               >
    <fx:Script>
        <![CDATA[
            import dstrube.NF1;
            import dstrube.NF2;
            import dstrube.NF3;
            [Bindable]
            public var s:String = "";
            protected  function init():void{
                var value:String = "5558049.90360013";
                s = "spark.formatters.NumberFormatter = " + NF1.get(value); //5,558,049.90
                s += "\n flash.globalization.NumberFormatter = " + NF2.get(value);//5,558,049,00
                s += "\n mx.formatters.NumberFormatter = " + NF3.get(value); //5,558,049.90360013
            }
        ]]>
    </fx:Script>
        <s:TextArea id="textArea" text="{s}" width="100%" height="100%" />
</s:Application>

三个 NumberFormatter 中最聪明的是 flash.globalization.NumberFormatter 用于识别小数分隔符,但它舍入不正确,显示 5,558,049,00 而不是 5,558,049,90

有任何想法吗?

4

2 回答 2

6

您可以:

  • 显式设置格式化程序的属性将为您提供所需的输出。
  • 将格式化程序设置为使用默认语言环境。

            [Bindable] protected var formatted:String;
    
            protected function init(event:FlexEvent):void
            {
                var formatter:NumberFormatter = new NumberFormatter();
    
                // Option 1 set explicitly
                formatter.decimalSeparator = ",";
                formatter.fractionalDigits = 2;
                formatter.trailingZeros = true;
    
                // Option 2 set default locale to be the locale
                formatter.setStyle("locale", LocaleID.DEFAULT);
    
                formatted = formatter.format("5558049.90360013");
            }
    
        ]]>
    </fx:Script>
    
    <s:Label text="{formatted}" />
    

输出为“5,558,049,90”。

于 2013-01-14T22:01:22.800 回答
1

这种方式将显示操作系统设置中设置的任何小数点分隔符。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               initialize="init(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import spark.formatters.NumberFormatter;
            import flash.globalization.NumberFormatter;

            [Bindable] protected var formatted:String;

            protected function init(event:FlexEvent):void
            {
                var formatter:spark.formatters.NumberFormatter = new spark.formatters.NumberFormatter();
                var nf2:flash.globalization.NumberFormatter = new flash.globalization.NumberFormatter("");

                formatter.decimalSeparator = nf2.decimalSeparator;
                formatter.fractionalDigits = 2;
                formatter.trailingZeros = true;

                formatted = formatter.format("5558049.90360013");
            }

        ]]>
    </fx:Script>

    <s:Label text="{formatted}" />
</s:Application>
于 2013-01-15T15:52:09.290 回答