加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:44:21 所属栏目:百科 来源:网络整理
导读:更改我的操作系统的小数分隔符后,如下所述: ( http://blogmines.com/blog/2010/03/11/how-to-change-the-decimal-separator-in-excel-2010/),我想在Flex中显示一个数字,它使用逗号分别表示千位分隔符和小数分隔符.听起来很简单吧? 我尝试使用Flex提供的三
更改我的操作系统的小数分隔符后,如下所述:
( 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);
        }
    }
}

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,049,00
                s += "n mx.formatters.NumberFormatter = " + NF3.get(value); //5,049.90360013
            }
        ]]>
    </fx:Script>
        <s:TextArea id="textArea" text="{s}" width="100%" height="100%" />
</s:Application>

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

有任何想法吗?

解决方法

你可以:

>明确设置格式化程序的属性将为您提供
您需要的输出.
>将格式化程序设置为使用默认语言环境.

????
????????

[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,90”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读