如何在Flex中显示带有不同小数分隔符的数字

dst*_*ube 3 apache-flex numberformatter

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

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

最后,主要

<?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>
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

Jus*_*ean 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}" />
    
    Run Code Online (Sandbox Code Playgroud)

输出为"5,558,049,90".